2009-05-18 7 views
0

RRを使用して私のコントローラのRSpecを書いてください。RailsのRRフレームワーク:instance_ofの複数の呼び出し

私は、コードを次のように書いた:まだ元方法を承認呼び出すときしかし、RRスタブのみメソッドを展開

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 

登録クラスのすべてのインスタンスに対して両方のメソッド呼び出しをスタブするためにはどのような構文を使用しますか?

UPDATE: 私は[モカ]私の知る限りでは

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

答えて

-1

で望ましい結果をachivied、RSpecのモックのは、あなたがそれを行うことはできません。すべてのインスタンスをスタブする必要がありますか?スペックに返されるどのようなオブジェクトあなたがコントロールRegistrationクラスのfindメソッドをスタブすることにより

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 

:私は通常、このパターンに従ってください。

+0

はそれが –

+1

質問はRRスタブではなく、RSpecのスタブについてです私のために役立ちましたありがとうございました。 – Peeja

+1

ああ - 私はそれを逃した - より良く読むことを学ぶべきである – jcfischer

関連する問題