简体   繁体   English

RSpec带有重音符的栏杆邮件测试

[英]RSpec rails mailer testing with accented character

I'm trying to make a test for a Rails Mailer and when the string that I'm trying to match have some special characters or accents it always breaks. 我正在尝试对Rails Mailer进行测试,当我尝试匹配的字符串具有某些特殊字符或重音符时,它总是会中断。

My test: 我的测试:

RSpec.describe UsersMailer, :type => :mailer do

  let(:mail) { UsersMailer.create(@user) }

  it 'includes first_name' do
    expect(mail.body.encoded).to match("#{HTMLEntities.new.encode('Félix', :named)}")
  end

end

My email view: 我的电子邮件视图:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
  <%= "#{HTMLEntities.new.encode(@user.first_name, :named)}" %>
</body>
</html>

The differences between them: 它们之间的区别:

test: F&eacute;lix 测试: F&eacute;lix

view: F&amp;eacute;lix 视图: F&amp;eacute;lix

When I do it without HTMLEntities, my differences are: 在没有HTMLEntities的情况下,我的区别是:

test: Félix 测试: Félix

view: F=C3=A9lix 视图: F=C3=A9lix

I'm using Rails 4.2.rc1, don't know if that makes any difference. 我正在使用Rails 4.2.rc1,不知道这有什么区别。

Solved it! 解决了! Just added a html_safe to my email view: 刚刚在我的电子邮件视图中添加了html_safe:

<%= "#{HTMLEntities.new.encode(@user.first_name, :named).html_safe}" %>

Was forgetting that. 忘记了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM