[rspec-users] Mocking User.find( :all, :limit => 10 )

David Chelimsky dchelimsky at gmail.com
Mon Jul 9 11:42:58 EDT 2007


On 7/9/07, Josh Knowles <joshknowles at gmail.com> wrote:
>
>
> On 7/9/07, Johan Sørensen <johan at johansorensen.com> wrote:
> >
> >
> >
> >
> >
> > Is there a way in RSpec to check for a specific hash key (or evaluating
> other things along those lines) in the parameter expectation using the
> should_receive(:find).with(...) construct?
>
>
> As opposed to getting into complicated mocking scenarios, consider
> abstracting this logic into your model:
> http://blog.caboo.se/articles/2007/6/19/rspec-notes-from-the-trenches-2

Agreed that this is a good approach in terms of dealing w/ rails
models. As for the question of isolating specific key/value pairs in a
hash, you could do this:

obj.should_receive(:message) do |*args|
  hash = args.pop
  hash[interesting_key].should == expected_value
end

It's not pretty, but it works.

The other thing you might consider is a custom matcher. Something like:

obj.should_receive(:message).with(has_key_value(key, value))

The mock framework can use these just as the expectation framework -
though mocks rely on == instead of matches.

David

>
>
>
>
> --
> Josh Knowles
> joshknowles at gmail.com
> http://joshknowles.com
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list