Rspec allow and expect the same method with different arguments
Asked Answered
T

1

10

I want to test my method which runs method Temp::Service.run two times inside it:

module Temp
  class Service

    def self.do_job
      # first call step 1
      run("step1", {"arg1"=> "v1", "arg2"=>"v2"})


      # second call step 2
      run("step2", {"arg3"=> "v3"})


    end

    def self.run(name, p)
      # do smth

      return true
    end


  end
end

I want to test arguments provided to second call of method :run with first argument 'step2' while I want to ignore the first call of the same method :run but with first argument 'step1'.

I have the RSpec test

RSpec.describe "My spec", :type => :request do

  describe 'method' do
    it 'should call' do

      # skip this
      allow(Temp::Service).to receive(:run).with('step1', anything).and_return(true)

      # check this
      expect(Temp::Service).to receive(:run) do |name, p|
        expect(name).to eq 'step2'

        # check p
        expect(p['arg3']).not_to be_nil

      end


      # do the job
      Temp::Service.do_job

    end
  end
end

but I got error

expected: "step2"
     got: "step1"

(compared using ==)

How to correctly use allow and expect for the same method ?

Toliver answered 16/5, 2017 at 13:42 Comment(2)
expectations will check for you the parameters sent, so, you don't need to do what you're doing in the block. Just use expect(Temp::Service).to receive(:run).with('step2', "arg3" => "v3") { true }.Lekishalela
I want to make more complicated checks over second argument. So I made it with block. Like this ` ['arg3', 'arg4'].each do |arg_good| expect(p[arg_good]).not_to be_nil end`Toliver
M
5

Seems like you are missing the .with('step2', anything)

it 'should call' do

  allow(Temp::Service).to receive(:run).with('step1', anything).and_return(true)

  # Append `.with('step2', anything)` here
  expect(Temp::Service).to receive(:run).with('step2', anything) do |name, p|
    expect(name).to eq 'step2' # you might not need this anymore as it is always gonna be 'step2'
    expect(p['arg3']).not_to be_nil
  end

  Temp::Service.do_job
end
Mcalister answered 16/5, 2017 at 14:6 Comment(1)
@MaxIvak no prob! :)Mcalister

© 2022 - 2024 — McMap. All rights reserved.