2013-08-20 10 views
13

RSpecの - 私は、モジュール内のメソッドをスタブするにはどうすればよいのスタブモジュール方式

module SomeModule 
    def method_one 
     # do stuff 
     something = method_two(some_arg) 
     # so more stuff 
    end 

    def method_two(arg) 
     # do stuff 
    end 
end 

私は隔離罰金にmethod_twoをテストすることができます。

私はmethod_twoの戻り値をスタブすることにより、あまりにも孤立してmethod_oneをテストしたいと思います:

shared_examples_for SomeModule do 
    it 'does something exciting' do 
     # neither of the below work 
     # SomeModule.should_receive(:method_two).and_return('MANUAL') 
     # SomeModule.stub(:method_two).and_return('MANUAL') 

     # expect(described_class.new.method_one).to eq(some_value) 
    end 
end 

describe SomeController do 
    include_examples SomeModule 
end 

SomeControllerに含まれているSomeModuleで仕様が失敗method_twoが例外をスローするので(それはデシベルを行うにしようシードされていないルックアップ)。

method_oneの中で呼び出されたときに、どのようにしてmethod_twoをスタブすることができますか?

+0

深刻な私の作品?なぜdownvote? – Harry

+0

このモジュールから何を期待していますか? mixinや、 'SomeModule.foo'のようなクラスレベルのメソッドを呼び出すことができますか? – apneadiving

+2

心を持ちなさい、ジョージ。質問の下降は私の意見では一般的に悪い考えですが、良いことは、不適切なものが頻繁に相殺するアップフォースを刺激することです。 –

答えて

2
shared_examples_for SomeModule do 
    let(:instance) { described_class.new } 

    it 'does something exciting' do 
    instance.should_receive(:method_two).and_return('MANUAL') 
    expect(instance.method_one).to eq(some_value) 
    end 
end 

describe SomeController do 
    include_examples SomeModule 
end 
+15

何が起こっているのか、これについての理由を説明してください。 –

関連する問題