2012-01-07 11 views
2

RSpecでテストしているRails 3アプリがあります。私は最高の私のコントローラの仕様にMustMockのインスタンスを模擬することができますどのようにRailsコントローラ仕様のモック外部クラス

class FooController < ApplicationController 
    def myaction 
    mockme = MustMock.new 
    @foobar = mockme.do_something 
    end 
end 

などの外部クラスMustMockを使用してコントローラを持っていますか?

答えて

5
describe FooController do 
    specify :myaction do 
    MustMock.should_receive(:new) 
      .and_return(stub :do_something => :something) 
    get :myaction 
    assigns[:foobar].should == :something 
    end 
end 
+0

+1、私はこのブロックのフォームがたくさん好きです。私はあなたのスタイルをコピーするつもりです! :) – iain

2

あなたはこれを試みることができる:

it "does something in myaction" do 
    my_stub = stub() 
    my_stub.should_receive(:do_something).once.and_return(:foobar) 
    MustMock.stub(:new => my_stub) 
    get :myaction 
    response.should be_success 
end 
+0

マイナーな屁理屈を - 私はshared_exampleに 'response.should be_success'を置く傾向があるだろう、ためにそれをしたいために非常に一般的であるようにページ。 YMMV – iain

+1

はい、テストを改善する方法はいくつかあります。私は、OPのコントローラーアクションがより複雑であり、問​​題を最小限の実証可能なアクションに蒸留していると思われます。 – Finbarr

+0

は完全に同意します、私は本当にマイナーを意味しました:) – iain

関連する問題