使用RSPec进行模拟/存根系统调用

这是辅助模块:

module SendingMailHelper def send_mail(subject, body) to_email_id = " abc@gmail.com" cc_email_id = "def@gmail.com" html_message = %{#{body}} 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 = %{www.google.com} 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: 

我还想以这样的方式模拟它,即mocked mutt返回false两次,并且在第三次调用中返回false以测试重试机制。 有没有办法做到这一点?

 require_relative './sending_mail_helper' require 'rspec/mocks/standalone' describe SendingMailHelper do let(:helper) do Class.new do include SendingMailHelper attr_reader :system_calls def system(*args) @system_calls ||= [] @system_calls << args [false, false, true][(@system_calls.size - 1) % 3] end end.new end it "test the call is made" do html_message = %{www.google.com} to_email_id = " abc@gmail.com" cc_email_id = "def@gmail.com" subject = "test e-mail" helper.send_mail("test e-mail","www.google.com") # check it was called once helper.system_calls.size should eq 1 # check it was called with appropriete arguments helper.system_calls.last.first.should eq %{echo "#{html_message}" | mutt -e "set content_type=text/html" -s "#{subject}" #{cc_email_id} -- #{to_email_id}} end it "retries the command until it succeded" do helper.send_mail("test e-mail","www.google.com") helper.system_calls.size.should eq 3 end end 

你可以使用这个小黑客。 实际上,这几乎就是存根和模拟监视函数调用。使用rspec-mock工具运行测试失败严重。通常我建议你不要测试系统调用它实际上会在你的测试中带来很多耦合并且实现并使代码更难维护。另一个建议我会给你使用一些ruby gem来处理你试图使用sys调用命令的电子邮件function。最后抱歉使用shoulds但我已经老了我的计算机上有rspec gem,缺少新的expect语法。