16

モジュール内でメソッドをスタブするにはどうすればよいですか:

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_oneの戻り値をスタブ化して、単独でテストしたいと思いますmethod_two:

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

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

method_two内で呼び出されたときにスタブするにはどうすればよいmethod_oneですか?

4

2 に答える 2

3
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
于 2013-09-20T18:22:02.870 に答える