当单引号出现时,RSpec`match`失败

时间:2016-04-07 07:58:50

标签: ruby-on-rails ruby rspec

当匹配器参数中存在偶然的单引号(由Faker生成)时,RSpec示例失败。

规范示例本身:

it 'contains organizer name' do
  expect(mail.body.encoded).to match organizer.name
end

输出示例:

1) PriorityRequestMailer#prioritize body contains organizer name
 Failure/Error: expect(mail.body.encoded).to match organizer.name
   expected "<html>...<a href=\"http://zieme-streich_905/events\">Gage O'Reilly</a>...</html>\r\n" to match "Gage O'Reilly"
   Diff:
   @@ -1,2 +1,66 @@
   -Gage O'Reilly
   +<html>
   ....

看起来名称Gage O'Reilly存在于正文中,但无论如何它都会失败。这个问题的原因是什么?

2 个答案:

答案 0 :(得分:3)

You don't typically want to match on encoded. In fact the docs for that method say

Calling this directly is not a good idea, but supported for compatibility

The encoded could be in a different encoding, converted to quoted-printable etc. A better idea is to match against decoded. Even better, you can do

expect(mail.body).to match(...)

since the body object already has a match method that does the right thing.

答案 1 :(得分:2)

也许是这样?

expect(mail.body.encoded).to include organiser.name