jos*_*ine 4 ruby regex unit-testing rspec rspec-rails
RSpec提供了参数regex匹配器,如下例所示:
mock_smtp.should_receive(:send_message).with(anything, anything, /@just-testing.com/)
Run Code Online (Sandbox Code Playgroud)
这很好,并允许我捕获与正则表达式匹配的参数,这适用于示例(匹配字符串,以'@ just-testing.com'结尾).
我的问题是我不能让这个适用于这种类型的参数:
msgstr = <<END_OF_MESSAGE
From: me <#{from_email}>
To: #{to_name} <#{to_email}>
Subject: #{subject}
MIME-Version: 1.0
Content-type: text/html
<!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">...</html>
END_OF_MESSAGE
Run Code Online (Sandbox Code Playgroud)
使用正则表达式可以捕获msgstr变量中的任何内容.
如何测试msgstr变量中的值?
不确定你有什么问题,但以下工作正常:
from_email = to_email = subject = to_name = "foobar@just-testing.com"
msgstr = <<END_OF_MESSAGE
From: me <#{from_email}>
To: #{to_name} <#{to_email}>
Subject: #{subject}
MIME-Version: 1.0
Content-type: text/html
<!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">...</html>
END_OF_MESSAGE
def foo(arg)
end
describe "" do
it "" do
expect(self).to receive(:foo).with(/@just-testing.com/)
foo(msgstr)
end
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3093 次 |
最近记录: |