Search code examples
ruby-on-rails-3rspec2rspec-rails

Using RSpec, how can I test that a method is called with instance of a class but with specific values on specific attributes?


I want to write a test like the following:

account = Account.find(1)
@controller.should_receive(:authorize!).with(:create, instance_of(User, :account_id => account.id))

(exact example on testing the cancan gem on a controller of mine)

I know that the part :account_id => account.id is invalid.

My question goes here:

How can I call the instance_of but at the same time test the attributes of the instance to have specific values?

I hope that this is clear. Otherwise, let me know, how can I make it more clear.

Thanks in advance

Panayotis


Solution

  • You may use a block for more control on the expected arguments, more info: Argument matchers