2013-03-14 46 views
0

基本上我想确保method_1和method_2应该从过程方法中调用。如何在下面的代码中为流程方法编写rspec?

def process 
     begin 
      method_1 if some_condition 
      method_2 if some_condition  
      self.update_attribute(:status,DONE) 
     rescue=>e 
      self.update_attribute(:status,ERROR) 
      p e 
     end 
    end 

def method_1 
#some code 
end 

def method_2 
#some code 
end 

回答

1

试试这个:

it "should call #method_1" do 
    YourClass.should_receive(:method_1) 
    YourClass.process 
end 

it "should call #method_2" do 
    YourClass.should_receive(:method_2) 
    YourClass.process 
end 

我假设这些都是类方法。

如果这些实例方法,你可以做YourClass.any_instance.should_receive(...)your_instance.should_receive(...)

更多信息,请参见http://rubydoc.info/gems/rspec-mocks/frames

编辑:

should_receive也将存根方法。这将取消存根,并调用方法:

YourClass.should_receive(:method_2).and_call_original 
+0

谢谢山姆,它为我工作。 – vivekporwal04 2013-03-15 06:08:16

+0

没问题。不要忘记标记答案是正确的:) – Sam 2013-03-15 12:04:51

相关问题