Subscription model-------------------------------------------- class Subscription < ActiveRecord::Base has_one :user def can_send_message? if user.sent_messages.count < limit true else false end end end Subscription model spec-------------------------------------------- require ''spec_helper'' describe Subscription do describe "#can_send_message?" do before(:each) do @subscription = Subscription.new(:limit => 10) @zach = User.create! :subscription => @subscription end context "when a user has not reached the subscription limit for the month" do it "returns true" do @zach.sent_messages.stub(:count).and_return(9) # this isn''t working @subscription.can_send_message?.should == true end end context "when a user has reached the subscription limit for the month" do it "returns false" do @zach.sent_messages.stub(:count).and_return(10) # this isn''t working @subscription.can_send_message?.should == false end end end end ================ When I run rspec spec for these examples, the first example passes because user.sent_messages.count is ZERO. The second example fails... Any ideas why? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/p6rVhv8rBC8J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chirag Singhal
2011-Jun-23 14:50 UTC
Re: RSpec book help; why won''t this stub method work?
count is a class method, so maybe you should try stubbing it on the Message class -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1JezJOD5dvsJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That did it! Thanks - I didn''t know class methods required that. Message.stub(:count).and_return(9) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/zG7UqWDf4MsJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.