For example: it ''should not validate if nil'' do @account.name = nil @account.should_not be_valid @account.errors[:name][0].should eq ''can\''t be blank'' end Is this good practice? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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.
Anthony Smith wrote in post #960202:> For example: > > it ''should not validate if nil'' do > @account.name = nil > @account.should_not be_validThose two lines are good practice: you''re testing that the object is invalid when you want it to be.> @account.errors[:name][0].should eq ''can\''t be blank''That line seems unnecessary: you''re testing the Rails framework, which is already well tested.> end > > Is this good practice?Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Nov 8, 2010 at 5:01 PM, Anthony Smith <anthony-pO6LffClugqiCYHjmAE91gC/G2K4zDHf@public.gmane.org>wrote:> For example: > > it ''should not validate if nil'' do > @account.name = nil > @account.should_not be_valid > @account.errors[:name][0].should eq ''can\''t be blank'' > end >I do - my discipline is whenever I am modifying a model I will write my outcomes first - and it does tend to find problems, especially when things start getting more complex.> > Is this good practice? > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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.
My thought is that it verifies you''re receiving the error you expected and not an artifact from another issue. So my intention is not to test Rails but to make sure my tests are doing what I''m expecting. Does that make sense or am I just paranoid? On Nov 8, 2010, at 6:14 PM, Marnen Laibow-Koser wrote:> Anthony Smith wrote in post #960202: >> For example: >> >> it ''should not validate if nil'' do >> @account.name = nil >> @account.should_not be_valid > > Those two lines are good practice: you''re testing that the object is > invalid when you want it to be. > >> @account.errors[:name][0].should eq ''can\''t be blank'' > > That line seems unnecessary: you''re testing the Rails framework, which > is already well tested. > >> end >> >> Is this good practice? > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > 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. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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.
On Nov 8, 2010, at 3:28 PM, Anthony Smith wrote:> My thought is that it verifies you''re receiving the error you expected and not an artifact from another issue. So my intention is not to test Rails but to make sure my tests are doing what I''m expecting. Does that make sense or am I just paranoid?Both :) Instead of testing the specific error message which could change you could test to make sure there is N (or at least N) number of errors on @account.name. That would solve your issue of it being invalid for some unrelated reason.> On Nov 8, 2010, at 6:14 PM, Marnen Laibow-Koser wrote: > >> Anthony Smith wrote in post #960202: >>> For example: >>> >>> it ''should not validate if nil'' do >>> @account.name = nil >>> @account.should_not be_valid >> >> Those two lines are good practice: you''re testing that the object is >> invalid when you want it to be. >> >>> @account.errors[:name][0].should eq ''can\''t be blank'' >> >> That line seems unnecessary: you''re testing the Rails framework, which >> is already well tested. >> >>> end >>> >>> Is this good practice? >> >> Best, >> -- >> Marnen Laibow-Koser >> http://www.marnen.org >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> 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. >> > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > 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. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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.
Philip Hallstrom wrote in post #960209:> On Nov 8, 2010, at 3:28 PM, Anthony Smith wrote: > >> My thought is that it verifies you''re receiving the error you expected and not > an artifact from another issue. So my intention is not to test Rails but > to make > sure my tests are doing what I''m expecting. Does that make sense or am I > just > paranoid? > > Both :) Instead of testing the specific error message which could > change you could test to make sure there is N (or at least N) number of > errors on @account.name. That would solve your issue of it being > invalid for some unrelated reason.That''s still testing Rails instead of your code. Instead, here''s what I do: before :each do @account = Account.make # from a factory with valid values end it "should be valid with valid values" @account.should be_valid # now we have our baseline end it "should not be valid without a name" @account.name = nil @account.should_not be_valid end ...and so on. This is the best stolidity I''ve been able to figure out. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote in post #960256: [...]> This is the best stolidity I''ve been able to figure out.iPhone autocorrect strikes again. I meant "atomicity", not "stolidity".> > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Pankowecki (rupert)
2010-Nov-09 10:22 UTC
Re: Does anyone test errors in their specs?
Here is my way: require ''mocha'' require ''shoulda'' should "not be valid without a name" do @account.name = nil @account.errors.expects(:add).with(:blank) @account.valid? end Testing messages is clumsy (especially with i18n) so I started testing that proper error was added (not propert message generated). Robert Pankowecki -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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.