Pete Campbell
2011-Mar-17 21:25 UTC
[rspec-users] 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! -- Posted via http://www.ruby-forum.com/.
Pete Campbell
2011-Mar-17 21:55 UTC
[rspec-users] Counter.should_not_receive(:message) not working?
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 -- Posted via http://www.ruby-forum.com/.
Michael Guterl
2011-Mar-17 22:31 UTC
[rspec-users] Counter.should_not_receive(:message) not working?
On Thu, Mar 17, 2011 at 5:25 PM, Pete Campbell <lists at ruby-forum.com> 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_meShould this be params[:count_me]? ---^ Best, Michael Guterl
Justin Ko
2011-Mar-18 00:53 UTC
[rspec-users] Counter.should_not_receive(:message) not working?
On Thu, Mar 17, 2011 at 2:55 PM, Pete Campbell <lists at ruby-forum.com> wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110317/7d3f99b0/attachment.html>
Pete Campbell
2011-Mar-18 01:24 UTC
[rspec-users] Counter.should_not_receive(:message) not working?
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. -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2011-Mar-18 03:53 UTC
[rspec-users] Counter.should_not_receive(:message) not working?
On Mar 17, 2011, at 8:24 PM, Pete Campbell wrote:> 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.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