Подтвердить что ты не робот

Метод 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_one в изоляции, 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 генерирует исключение (он пытается выполнить поиск db, который не был посеян).

Как я могу method_two когда он вызван в method_one?

4b9b3361

Ответ 1

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