Rspec 3.0 How to make fun of a method that replaces a parameter, but without a return value?

I searched a lot and just can’t understand, although this seems basic. Here is a simplified example of what I want to do.

Create a simple method that does something but returns nothing, for example:

class Test def test_method(param) puts param end test_method("hello") end 

But in my rspec test, I need to pass another parameter, for example "goodbye" instead of "hello". I know this is related to stubs and layouts, and I am looking at the documentation, but I can’t understand: https://relishapp.com/rspec/rspec-mocks/v/3-0/docs/method-stubs

If I do this:

 @test = Test.new allow(@test).to_receive(:test_method).with("goodbye") 

he tells me to cut the default value, but I cannot figure out how to do it correctly.

Error message:

 received :test_method with unexpected arguments expected: ("hello") got: ("goodbye") Please stub a default value first if message might be received with other args as well. 

I am using rspec 3.0 and calling something like

 @test.stub(:test_method) 

is not allowed.

+7
ruby mocking rspec stubbing
source share
2 answers

In your example, since you do not need to check the actual result of test_method , it only calls puts , passing in param , I would just check by setting the wait and executing the method:

 class Test def test_method(param) puts param end end describe Test do let(:test) { Test.new } it 'says hello via expectation' do expect(test).to receive(:puts).with('hello') test.test_method('hello') end it 'says goodbye via expectation' do expect(test).to receive(:puts).with('goodbye') test.test_method('goodbye') end end 

It seems that what you are trying to do is configure the test spy by the method, but then I think that you configure the method will close one level too high (on test_method itself, and not on the puts call inside test_method ). If you put a stub on the puts call, your tests should pass:

 describe Test do let(:test) { Test.new } it 'says hello using a test spy' do allow(test).to receive(:puts).with('hello') test.test_method('hello') expect(test).to have_received(:puts).with('hello') end it 'says goodbye using a test spy' do allow(test).to receive(:puts).with('goodbye') test.test_method('goodbye') expect(test).to have_received(:puts).with('goodbye') end end 
+1
source share

How to set the default value explained in

and_call_original can configure a default response that can be overridden for specific arguments

 require 'calculator' RSpec.describe "and_call_original" do it "can be overriden for specific arguments using #with" do allow(Calculator).to receive(:add).and_call_original allow(Calculator).to receive(:add).with(2, 3).and_return(-5) expect(Calculator.add(2, 2)).to eq(4) expect(Calculator.add(2, 3)).to eq(-5) end end 

The source from where I found out about this can be found at https://makandracards.com/makandra/30543-rspec-only-stub-a-method-when-a-particular-argument-is-passed

+5
source share

All Articles