Topic: How to test whether present? is called in rspec?

Dear all,

In rspec, how can I test whether present? or blank? (or similar) is called? Normally I'd do something like this:

controller.current_user.sanity.should_receive(:present?)

But this doesn't work as 'sanity' can be nil, so rspec spits out a rather odd message on failure:

       (nil).present?(any args)
           expected: 1 time
           received: 9 times

How can I test whether the user's sanity was checked as present?

Many thanks.

Re: How to test whether present? is called in rspec?

Can you try it as follows:

double.should_receive(:msg).once
double.should_receive(:msg).twice
double.should_receive(:msg).exactly(n).times
double.should_receive(:msg).at_least(:once)
double.should_receive(:msg).at_least(:twice)
double.should_receive(:msg).at_least(n).times
double.should_receive(:msg).at_most(:once)
double.should_receive(:msg).at_most(:twice)
double.should_receive(:msg).at_most(n).times
double.should_receive(:msg).any_number_of_times

where you replace the 'double' by your mocked object and ':msg' by ':present?'.

Re: How to test whether present? is called in rspec?

Thanks for your reply.

I'm not sure why present? is called on nil 9 times, but that's not really the problem. I'd like to know whether there is some way of testing that the following code was called:

def debate
  return false if user.sanity.present?
  ...
end

user.sanity will return nil, but for the purpose of my test it's meant to. I just need to make sure that the debate method checks the user's sanity. Testing nil.present? isn't really good enough as it could be triggered by other code.