Rubyunit testing技术,Mocking和Stubbing

我被招募为SW Dev,我正在尝试使用RSPEC和RR进行unit testing,但是很难决定特定策略,主要是因为我被分配给已经编码的unit testing书面。

考虑以下代码,它是一个名为method1的大方法的一部分:

if (["5234541252", "6236253223"].include?(self.id)) self.DoCheck logs.add 'Doing check', "id = #{self.id}" return end 

这部分方法的相关unit testing将是这样的:

"should do check only if id = 5234541252 or 6236253223"

但我遇到了几个问题,基本上涉及最佳实践,例如:

如何使用RR和RSPEC检查是否已从“method1”中调用DoCheck?

我尝试过使用dont_allow(Object).DoCheck但是它不起作用。

 describe :do_route do it "should do check only if id = 5234541252 or 6236253223" do user = Factory(:User) user.id = "5234541252" dont_allow(user).DoCheck user.method1 end 

如果“DoCheck”被调用,还有其他方法可以得出结论吗?

您可以在测试对象上使用“Partial Mocking”技术。 这意味着您需要设置user.method1user发送DoCheck消息的期望。 RSpec可以使用should_receive()方法设置此期望值。 如果不满足预期,测试将失败。

请尝试以下代码:

 describe :do_route do it "should do check if id = 5234541252 or 6236253223" do user = Factory(:User) user.id = "5234541252" user.should_receive(:DoCheck) # set expectation that DoCheck will be called user.method1 # execute end end 

对于RR,这种期望可能如下所示:

 describe :do_route do it "should do check if id = 5234541252 or 6236253223" do user = Factory(:User) user.id = "5234541252" mock(user).DoCheck # set expectation that DoCheck will be called user.method1 # execute end end 

更新

但要小心使用模拟对象和部分模拟,因为它们会使测试代码与测试下代码的当前行为紧密耦合。 如果您将来要重构此代码,您可能会遇到很多失败的测试(那些使用模拟的测试)。 尝试在没有模拟(输入 – 输出)的情况​​下测试代码,并且只有在明确解释被测试类的意图时才使用模拟。 当你用mock编写测试时,总是问自己,你真的需要断言这个方法调用另一个方法,或者你真正关心的是方法的输出或其他东西。

希望这可以帮助!