Counter.should_not_receive(:message) not working?

I’m trying to test a Rails controller and am having trouble getting
SHOULD_NOT_RECEIVE to work on a class method. I can never get
Counter.should_not_receive(:xxx) to fail when the message ‘xxx’ is sent.

Controller:
def show
@counter = Counter.increment if count_me
end

RSpec:
describe ‘Show’ do
it “should INCREMENT only when count-me is true” do
Counter.should_receive(:increment)
Counter.should_receive(:increment).never
Counter.should_not_receive(:increment)
get :show, :count_me => ‘true’
end
end

Why does this pass? Counter.should_receive(:increment) passes
correctly (or fails if COUNT_ME isn’t set correctly), but shouldn’t the
next 2 statements fail?

This is in Rails 2.3.11 & MongoDB.

Thanks!

Even simpler…

describe ‘Show’ do
it ‘should fail because increment is called’ do
Counter.should_not_receive(:increment) # Incorrectly passes
get :show, :count_me => ‘true’
end
it ‘should fail because increment is not called’ do
Counter.should_receive(:increment) # Correctly fails
get :show
end
end

On Thu, Mar 17, 2011 at 5:25 PM, Pete C. [email protected]
wrote:

I’m trying to test a Rails controller and am having trouble getting
SHOULD_NOT_RECEIVE to work on a class method. I can never get
Counter.should_not_receive(:xxx) to fail when the message ‘xxx’ is sent.

Controller:
def show
@counter = Counter.increment if count_me
Should this be params[:count_me]? —^

Best,
Michael G.

Justin Ko wrote in post #988060:

I would say “count_me” is not returning what you think it is. Try adding
“raise count_me.inspect” in the controller action to see if this is the
case.

Thanks for the feedback. The code snippet isn’t the exact code, it is
simplified to demonstrate the problem I’m having (or think that I’m
having). I know that the count_me values are set correctly when
experiencing this problem. I also know when the Counter methods are
executed or not (lots of ‘puts’!), so I’m sure that the Counter is
receiving the message but the .should_not_receive isn’t raising an
error.

I’m probably doing something incorrect w/r/t the way you’re supposed to
use mocks in RSpec, but so far I haven’t found what that is.

On Mar 17, 2011, at 8:24 PM, Pete C. wrote:

executed or not (lots of ‘puts’!), so I’m sure that the Counter is
receiving the message but the .should_not_receive isn’t raising an
error.

I’m probably doing something incorrect w/r/t the way you’re supposed to
use mocks in RSpec, but so far I haven’t found what that is.

Please post a single, complete example in a gist that we can copy into a
file and run so we can see the problem locally.

Cheers,
David

On Thu, Mar 17, 2011 at 2:55 PM, Pete C. [email protected]
wrote:

end
end


Posted via http://www.ruby-forum.com/.


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

I would say “count_me” is not returning what you think it is. Try adding
“raise count_me.inspect” in the controller action to see if this is the
case.