rspec-users-vzwcmIQjL/tcJFG9EKvDmT1X8v8AiJow@public.gmane.org
2008-Nov-27 15:06 UTC
attributes method not working?
This is probably basic but I''m not seeing it. I''m using rspec & rspec-rails 1.1.11 on Ubuntu, & rails 2.1.0. Here''s (the relevant parts of) my class I''m testing: class Adtag < ActiveRecord::Base # snip validates_inclusion_of :size, :in => IAB_SIZES validates_presence_of :code validates_presence_of :client_id validates_presence_of :campaign_id # snip end And here''s the spec: module AdtagSpecHelper def valid_adtag_attributes { :category => "shopping", :code => "goes", :client_id => 1, :campaign_id => 1, :size => "300x250" } end end describe Adtag do include AdtagSpecHelper before(:each) do @adtag = Adtag.create( valid_adtag_attributes ) @adtag.should be_valid end it "should be invalid without an approved size" do @adtag.attributes = valid_adtag_attributes.except(:size) # ONE @adtag.errors.on(:size).should eql("is not included in the list") # TWO @adtag.should have(1).error_on(:size) # THREE @adtag.should_not be_valid @adtag.size = "300x250" @adtag.should be_valid end end When I run this, the line with # ONE isn''t getting executed. the attributes= method doesn''t change the attributes of the @adtag object and I don''t know why - there is no update SQL command in the test log. I can get around this by changing that line to this: @adtag.update_attribute( :size, nil ) ...which works but then at the line with # THREE, another error appears: 1) ''Adtag should be invalid without an approved size'' FAILED expected "is not included in the list", got nil (using .eql?) ...and that is weird because the "validates_inclusion_of :size, :in => IAB_SIZES" in the model should complain, not give nil. All of this adds up to some general strangeness that seems pretty un-rspec-like. Any ideas or help is most welcome. -Jason
On Thu, Nov 27, 2008 at 9:06 AM, <rspec-users at jfrankov.otherinbox.com> wrote:> This is probably basic but I''m not seeing it. I''m using rspec & rspec-rails > 1.1.11 on Ubuntu, & rails 2.1.0. Here''s (the relevant parts of) my class I''m > testing: > > class Adtag < ActiveRecord::Base > > # snip > > validates_inclusion_of :size, :in => IAB_SIZES > validates_presence_of :code > validates_presence_of :client_id > validates_presence_of :campaign_id > > # snip > > end > > > > And here''s the spec: > > module AdtagSpecHelper > def valid_adtag_attributes > { > :category => "shopping", > :code => "<some adtag=''/code''>goes</here>", > :client_id => 1, > :campaign_id => 1, > :size => "300x250" > } > end > end > > describe Adtag do > include AdtagSpecHelper > > before(:each) do > @adtag = Adtag.create( valid_adtag_attributes ) > @adtag.should be_valid > end > > it "should be invalid without an approved size" do > @adtag.attributes = valid_adtag_attributes.except(:size) # ONEattributes= does NOT set any attributes that are not included in the hash. In this case it does not try to assign the value of :size. As for not showing in the test log, I believe (but I''m not sure) that AR will not run an SQL update unless attributes= changes the value of any of the attributes it assigns. In this case, that is not happening, so no query. Cheers, David> @adtag.errors.on(:size).should eql("is not included in the list") # TWO > @adtag.should have(1).error_on(:size) # THREE > @adtag.should_not be_valid > @adtag.size = "300x250" > @adtag.should be_valid > end > end > > When I run this, the line with # ONE isn''t getting executed. the attributes> method doesn''t change the attributes of the @adtag object and I don''t know > why - there is no update SQL command in the test log. I can get around this > by changing that line to this: > > @adtag.update_attribute( :size, nil ) > > ...which works but then at the line with # THREE, another error appears: > > 1) > ''Adtag should be invalid without an approved size'' FAILED > expected "is not included in the list", got nil (using .eql?) > > ...and that is weird because the "validates_inclusion_of :size, :in => > IAB_SIZES" in the model should complain, not give nil. > > > > All of this adds up to some general strangeness that seems pretty > un-rspec-like. Any ideas or help is most welcome. > > > > -Jason > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Thu, Nov 27, 2008 at 11:03 AM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Thu, Nov 27, 2008 at 9:06 AM, <rspec-users at jfrankov.otherinbox.com> > wrote: > > > it "should be invalid without an approved size" do > > @adtag.attributes = valid_adtag_attributes.except(:size) # ONE > > attributes= does NOT set any attributes that are not included in the > hash. In this case it does not try to assign the value of :size. > > As for not showing in the test log, I believe (but I''m not sure) that > AR will not run an SQL update unless attributes= changes the value of > any of the attributes it assigns. In this case, that is not happening, > so no query. > > > @adtag.errors.on(:size).should eql("is not included in the list") # > TWO > > @adtag.should have(1).error_on(:size) # THREE > > @adtag.should_not be_valid >And since it''s the valid? call which sets the errors, even if you address the problems pointed out by David, those errors expectations won''t be met if you do things in this order. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081127/627426e9/attachment.html>