2009-05-18 54 views
0

我想爲使用RR的控制器編寫RSpec。Rails RR Framework:多次調用instance_of

我寫了下面的代碼:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') 

describe RegistrationController do 

    it "should work" do 
     #deploy and approve are member functions 
     stub.instance_of(Registration).approve { true } 
     stub.instance_of(Registration).deploy { true } 
     post :register 
    end 
end 

然而RR存根只部署方法時,還叫原來批准方法。

我應該使用什麼語法來爲所有Registration類的實例存根方法調用?

UPDATE:與[摩卡] 我achivied期望的結果

Registration.any_instance.stubs(:deploy).returns(true) 
Registration.any_instance.stubs(:approve).returns(true) 

回答

-1

據我所知,RSpec的嘲弄不允許你這樣做。你確定,你需要存儲所有實例嗎?我通常遵循以下模式:

describe RegistrationController do 
    before(:each) do 
     @registration = mock_model(Registration, :approve => true, :deploy => true) 
     Registration.stub!(:find => @registration) 
     # now each call to Registration.find will return my mocked object 
    end 

    it "should work" do 
     post :register 
     reponse.should be_success 
    end 

    it "should call approve" do 
     @registration.should_receive(:approve).once.and_return(true) 
     post :register 
    end 

    # etc 
end 

通過對所控制的註冊類的find方法進行存根化,在規範中返回了哪些對象。

+0

謝謝你對我來說是 – 2009-06-28 14:08:48