How to expect some (but not all) arguments with RSpec should_receive?

前端 未结 3 864
渐次进展
渐次进展 2021-01-07 16:32
class Foo
  def bar(a, b)
  ...

Foo.should_receive( :bar )

expects bar to be called with any arguments.

Foo.should_receive( :bar )         


        
相关标签:
3条回答
  • 2021-01-07 17:11

    There's another way to do this, which is the block form of receive: https://relishapp.com/rspec/rspec-mocks/v/3-2/docs/configuring-responses/block-implementation#use-a-block-to-verify-arguments

    expect(Foo).to receive(:bar) do |args|
      expect(args[0]).to eq(:baz)
    end
    
    0 讨论(0)
  • 2021-01-07 17:12

    Use the anything matcher:

    Foo.should_receive(:bar).with(:baz, anything)
    
    0 讨论(0)
  • 2021-01-07 17:20

    For Rspec 1.3 anything doesn't work when your method is receiving a hash as an argument, so please try with hash_including(:key => val):

    Connectors::Scim::Preprocessors::Builder.
        should_receive(:build).
        with(
          hash_including(:connector => connector)
          ).
        and_return(preprocessor)
    }
    
    0 讨论(0)
提交回复
热议问题