[rspec-users] Fuzzy should_receive matching

David Chelimsky dchelimsky at gmail.com
Thu May 30 12:11:31 UTC 2013


On Thu, May 30, 2013 at 2:44 AM, Alexander Baronec <abonec at gmail.com> wrote:

> Hello.
> I have a test problem when I should test arguments in method not by exact
> matching buy with fuzzy matching.
>
> For example I have this test:
>
> require 'rspec'
>
> describe do
>   it do
>     o = Object.new
>     o.should_receive(:api_send).with mode: :say
>
>     o.api_send mode: :say, time: Time.now + rand(1..10), bans: { login1:
> {time: Time.now, expiration: Time.now + 100}}
>
  end
> end
>
> I want helper to test that method api_send receive mode: :say, time:
> Time.now + rand(1..10), bans: { login1: {time: Time.now, expiration:
> Time.now + 100}} but I do not care about what time exactly is.
>
> I want something like
>
> inspect_api o, mode: :say, time: Time, bans: { login:1: {time: Time,
> expiration: Time}}
>

See docs:

http://rubydoc.info/gems/rspec-mocks/file/README.md#Argument_Matchers
http://rubydoc.info/gems/rspec-mocks/RSpec/Mocks/ArgumentMatchers#hash_including-instance_method

(the info in the 2nd link needs to be added to the first - sorry)

You could do either of:

    o.should_receive(:api_send).with hash_including(mode: :say)
    o.should_receive(:api_send).with mode: :say, time: instance_of(Time),
:bans { login1: {time: instance_of(Time), expiration: instance_of(Time) }

HTH,
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20130530/3d4f29ef/attachment.html>


More information about the rspec-users mailing list