2013-03-14 45 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

相關問題