【问题标题】:How to properly do string concatenation in Rspec? [duplicate]如何在 Rspec 中正确地进行字符串连接? [复制]
【发布时间】:2015-08-25 15:56:03
【问题描述】:

我正在 Rspec 中测试一个邮件程序,并且遇到了字符串连接的怪癖。当我这样做时:

it 'renders the subject' do
  expect(mail.subject).to eq('#{order.firstname} <#{order.email}> has made an order.')
end

我收到以下消息:

OrderMailer instructions renders the subject
 Failure/Error: expect(mail.subject).to eq('#{order.firstname} <#{order.email}> has made an order.')

   expected: "\#{order.firstname} <\#{order.email}> has made an order."
        got: "Joe <joe@mctester.com> has made an order."

   (compared using ==)

但是,当我执行以下操作时,它会通过:

it 'renders the subject' do
  expect(mail.subject).to eq(order.firstname + " <" + order.email + "> has made an order.")
end

在 rspec 中连接的正确方法是什么?我发现第一种方法更容易操作,使用起来也更快。

【问题讨论】:

  • 不公平地否决这个问题。是的,这是一个红宝石菜鸟错误,但这不是一个坏问题。

标签: ruby-on-rails ruby rspec


【解决方案1】:

字符串插值仅适用于 ruby​​ 中的双引号:

it 'renders the subject' do
  expect(mail.subject).to eq("#{order.firstname} <#{order.email}> has made an order.")
end

应该可以的。

【讨论】:

  • 当然。我几乎一直在 ruby​​ 中使用双引号,所以我从来没有遇到过这种情况。
猜你喜欢
  • 2020-01-13
  • 2021-11-03
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多