Nikos Dimitrakopoulos
2008-Mar-18 23:26 UTC
[rspec-users] Problem with mocking a simple has_many relationship
Hi there all. Sorry if the question sounds silly but i''m rather new at the ''mocking'' stuff... So here is my problem (code helps more than talking): the model: class Item < ActiveRecord::Base ... has_many :images, :dependent => :destroy ... protected def validate errors.add(:images, "cannot be empty") if self.images.count end end the spec: describe Item do before(:each) do @item = Item.new end ... it "should have at least one image" do @item.should have(1).error_on(:images) @item.images.should_receive(:count).at_least(:once).and_return(1) @item.should have(0).errors_on(:images) end end Obviously i''m doing something wrong cause this doesn''t work: 1) ''Item should have at least one image'' FAILED expected 0 errors on :images, got 1 ./spec/models/item_spec.rb:60: script/spec:4: Finished in 0.341594 seconds 10 examples, 1 failure, 5 pending (ignore the rest) Any help would be more than welcome :) -- Posted via http://www.ruby-forum.com/.
James Deville
2008-Mar-18 23:37 UTC
[rspec-users] Problem with mocking a simple has_many relationship
On Mar 18, 2008, at 4:26 PM, Nikos Dimitrakopoulos wrote:> Hi there all. Sorry if the question sounds silly but i''m rather new at > the ''mocking'' stuff... So here is my problem (code helps more than > talking): > > the model: > > class Item < ActiveRecord::Base > ... > has_many :images, :dependent => :destroy > ... > > protected > > def validate > errors.add(:images, "cannot be empty") if self.images.count > end > end > > > the spec: > > describe Item do > before(:each) do > @item = Item.new > end > > ... > > it "should have at least one image" do > @item.should have(1).error_on(:images) > @item.images.should_receive(:count).at_least(:once).and_return(1) > @item.should have(0).errors_on(:images) > end > end >I would try: it "should have an error with no images" do @item.should have(1).error_on(:images) end it "should be okay with one image" do @item.stub!(:images).and_return(mock("images",:count => 1)) @item.should have(0).errors_on(:images) end JD> > Obviously i''m doing something wrong cause this doesn''t work: > > 1) > ''Item should have at least one image'' FAILED > expected 0 errors on :images, got 1 > ./spec/models/item_spec.rb:60: > script/spec:4: > > Finished in 0.341594 seconds > > 10 examples, 1 failure, 5 pending > > (ignore the rest) > > Any help would be more than welcome :) > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Rafael Mueller
2008-Mar-18 23:38 UTC
[rspec-users] Problem with mocking a simple has_many relationship
Hi Nikos, On Tue, Mar 18, 2008 at 8:26 PM, Nikos Dimitrakopoulos <lists at ruby-forum.com> wrote:> > the model: > class Item < ActiveRecord::Base > has_many :images, :dependent => :destroy > protected > def validate > errors.add(:images, "cannot be empty") if self.images.count > end > endself.images.count is always true, the Fixnum 0 (returned when theres no images) is true try on irb: foo = "bar" if [].size I guess the correct way is errors.add(:images, "cannot be empty") if self.images.size < 1 Regards, -- Rafael Mueller http://queroseragil.wordpress.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080318/18dfec24/attachment.html
James Deville
2008-Mar-18 23:47 UTC
[rspec-users] Problem with mocking a simple has_many relationship
Doh! I can''t believe that I missed that. You can also use if self.images.size.zero? Just a little more readable. James Deville On Mar 18, 2008, at 4:38 PM, Rafael Mueller wrote:> Hi Nikos, > > > On Tue, Mar 18, 2008 at 8:26 PM, Nikos Dimitrakopoulos <lists at ruby-forum.com > > wrote: > > the model: > class Item < ActiveRecord::Base > has_many :images, :dependent => :destroy > protected > def validate > errors.add(:images, "cannot be empty") if self.images.count > end > end > > self.images.count is always true, the Fixnum 0 (returned when theres > no images) is true > > try on irb: foo = "bar" if [].size > > I guess the correct way is errors.add(:images, "cannot be empty") if > self.images.size < 1 > > Regards, > -- > Rafael Mueller > http://queroseragil.wordpress.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080318/1b9f6552/attachment.html
Nikos Dimitrakopoulos
2008-Mar-18 23:48 UTC
[rspec-users] Problem with mocking a simple has_many relationship
Ok, i''m really stupid... i was taking a break (i''ve been on the pc for several hours) and the problem came to me before reading your posts :D Obviously the "if self.images.count" was waaaayyy too stupid! added the necessary == 0 and everything is fine... Thanks a lot anyway :) -- Posted via http://www.ruby-forum.com/.
Nikos Dimitrakopoulos
2008-Mar-18 23:49 UTC
[rspec-users] Problem with mocking a simple has_many relationship
James Deville wrote:> Doh! I can''t believe that I missed that. > > You can also use if self.images.size.zero? > > Just a little more readable. > > James DevilleYou were writing while i was writing :D nice one, i didn''t know the #zero? - way more readable :) -- Posted via http://www.ruby-forum.com/.
Nikos Dimitrakopoulos
2008-Mar-19 00:04 UTC
[rspec-users] Problem with mocking a simple has_many relationship
Ok, last one - for your interest validates_length_of can be used for this purpose. There is an open bug/patch on the rails trac regarding the :within parameter but the :minimum works fine for me. As for the example the method called on @item.images is #size and not #count so -> @item.images.should_receive(:size).and_return(1) P.S.: The bug on the rails trac: http://dev.rubyonrails.org/ticket/11295 -- Posted via http://www.ruby-forum.com/.