【发布时间】:2014-07-01 15:10:44
【问题描述】:
这是帮助模块:
module SendingMailHelper
def send_mail(subject, body)
to_email_id = " abc@gmail.com"
cc_email_id = "def@gmail.com"
html_message = %{<html><body>#{body}</body></html>}
flag=false
while(!flag) do
flag = system %{echo "#{html_message}" | mutt -e "set content_type=text/html" -s "#{subject}" #{cc_email_id} -- #{to_email_id}}
end
flag
end
end
我编写规范的方式如下,但它不起作用:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe SendingMailHelper do
it "test something" do
html_message = %{<html><body>www.google.com</body></html>}
to_email_id = " abc@gmail.com"
cc_email_id = "def@gmail.com"
subject = "test e-mail"
SendingMailHelper.expects(:system).with(%{echo "#{html_message}" | mutt -e "set content_type=text/html" -s "#{subject}" #{cc_email_id} -- #{to_email_id}}).returns(true).once
helper.send_mail("test e-mail","www.google.com").should==true
end
end
得到以下错误:
SendingMailHelper test something
Failure/Error: SendingMailHelper.expects(:system).with(%{echo "#{html_message}" | mutt -e "set content_type=text/html" -s "#{subject}" #{cc_email_id} -- #{to_email_id}}).returns(true).once
Mocha::ExpectationError:
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked:
我还想以这样一种方式模拟它,模拟 mutt 两次返回 false 并在第三次调用中返回 true 以测试重试机制。有没有办法做到这一点?
【问题讨论】:
-
试试
Kernel.expects(:system)... -
我应该如何修复上述测试用例?
标签: ruby-on-rails ruby rspec