The title of this post may be somewhat misleading; I am indeed UNIT testing this, and it works fine, however I''m also functional testing a POST to an action that is responsible for creating a new ActiveRecord object that''s associated with the "parent" via has_many :through. class Report < ActiveRecord::Base # ... has_many :report_notifiers has_many :notifiers, :through => :report_notifiers # ... end class ReportNotifier < ActiveRecord::Base # ... belongs_to :report # ... end So, creating a new report with a new notifier would be simple: @r = Report.new @r.notifiers << Notifier.create(:label => "something", :email => "foo-+RB1Aph5k6s@public.gmane.org") But what happens when a user submits invalid e-mail address? Well, of course, the validation fails. But how do I go about testing for the presence of that validation failure message in my functional test? Here''s my existing functional test: -------------------- def test_edit_with_invalid_notifiers m = Factory(:maintenance_report) # I dig factory_girl, great plugin post :edit, {:m => m, :id => m.id, :notifiers => [1, 2, 3, '''', 0, ''asdfasf'', "\n\n;'';'''''''';UPDATE notifiers SET label=\"hacked\""], :new_notifiers => "not a valid email!asdfasdfasf afd asdfasf 2 wgw@a asdf...\n\n\n\tsomevalidemail-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"} # ...now what? end --------------------- As you can see, my test gives the controller action invalid data, which will trigger an error when the validation is attempted. But, given that the Notifier class, which contains the validation, belongs_to the Report class, which is what I''m creating a new one of, how do I test to see if we get any errors? i.e. in a template I''d just have: <%= errors_for :maintenance_report %> ...which would theoretically contain a bullet point saying something about "invalid e-mail address". But how do I test that?
You should be able to do an assert_select looking for whatever the <%errors_for :maintenance_report %> generates for the error you are expecting. Or have I missed a subtlety of your question? Colin 2009/5/21 Phoenix Rising <PolarisRising-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > The title of this post may be somewhat misleading; I am indeed UNIT > testing this, and it works fine, however I''m also functional testing a > POST to an action that is responsible for creating a new ActiveRecord > object that''s associated with the "parent" via has_many :through. > > class Report < ActiveRecord::Base > # ... > has_many :report_notifiers > has_many :notifiers, :through => :report_notifiers > # ... > end > > class ReportNotifier < ActiveRecord::Base > # ... > belongs_to :report > # ... > end > > So, creating a new report with a new notifier would be simple: > @r = Report.new > @r.notifiers << Notifier.create(:label => "something", :email => > "foo-+RB1Aph5k6s@public.gmane.org") > > But what happens when a user submits invalid e-mail address? Well, of > course, the validation fails. But how do I go about testing for the > presence of that validation failure message in my functional test? > > Here''s my existing functional test: > -------------------- > def test_edit_with_invalid_notifiers > m = Factory(:maintenance_report) # I dig factory_girl, great > plugin > > post :edit, {:m => m, :id => m.id, :notifiers => [1, 2, 3, '''', 0, > ''asdfasf'', "\n\n;'';'''''''';UPDATE notifiers SET label=\"hacked\""], > :new_notifiers => "not a valid email!asdfasdfasf afd asdfasf 2 > wgw@a asdf...\n\n\n\tsomevalidemail-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"} > > # ...now what? > > end > --------------------- > > As you can see, my test gives the controller action invalid data, > which will trigger an error when the validation is attempted. But, > given that the Notifier class, which contains the validation, > belongs_to the Report class, which is what I''m creating a new one of, > how do I test to see if we get any errors? > > i.e. in a template I''d just have: > <%= errors_for :maintenance_report %> > > ...which would theoretically contain a bullet point saying something > about "invalid e-mail address". But how do I test that? > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phoenix Rising
2009-May-21 14:26 UTC
Re: Functional testing a has_many :through validation
Thanks for your reply Colin. In theory, there should be some way I can test for the presence or absence of errors on something like: @report.notifiers.errors.on (something?) The thing is, since the post creates a new record for the associated model, I''m not quite sure how to "access" that. On May 21, 1:21 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> You should be able to do an assert_select looking for whatever the <%> errors_for :maintenance_report %> generates for the error you are > expecting. Or have I missed a subtlety of your question? > > Colin > > 2009/5/21 Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > > > > The title of this post may be somewhat misleading; I am indeed UNIT > > testing this, and it works fine, however I''m also functional testing a > > POST to an action that is responsible for creating a new ActiveRecord > > object that''s associated with the "parent" via has_many :through. > > > class Report < ActiveRecord::Base > > # ... > > has_many :report_notifiers > > has_many :notifiers, :through => :report_notifiers > > # ... > > end > > > class ReportNotifier < ActiveRecord::Base > > # ... > > belongs_to :report > > # ... > > end > > > So, creating a new report with a new notifier would be simple: > > @r = Report.new > > @r.notifiers << Notifier.create(:label => "something", :email => > > "f...-+RB1Aph5k6s@public.gmane.org") > > > But what happens when a user submits invalid e-mail address? Well, of > > course, the validation fails. But how do I go about testing for the > > presence of that validation failure message in my functional test? > > > Here''s my existing functional test: > > -------------------- > > def test_edit_with_invalid_notifiers > > m = Factory(:maintenance_report) # I dig factory_girl, great > > plugin > > > post :edit, {:m => m, :id => m.id, :notifiers => [1, 2, 3, '''', 0, > > ''asdfasf'', "\n\n;'';'''''''';UPDATE notifiers SET label=\"hacked\""], > > :new_notifiers => "not a valid email!asdfasdfasf afd asdfasf 2 > > wgw@a asdf...\n\n\n\tsomevalidem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"} > > > # ...now what? > > > end > > --------------------- > > > As you can see, my test gives the controller action invalid data, > > which will trigger an error when the validation is attempted. But, > > given that the Notifier class, which contains the validation, > > belongs_to the Report class, which is what I''m creating a new one of, > > how do I test to see if we get any errors? > > > i.e. in a template I''d just have: > > <%= errors_for :maintenance_report %> > > > ...which would theoretically contain a bullet point saying something > > about "invalid e-mail address". But how do I test that?
I think maybe assert assigns(:report).notifiers.errors.on(:fieldname) where fieldname is the field that you have added the error on. Or something like that anyway. If it doesn''t work you could break into the test code with the ruby debugger at that point, then you can inspect the variables and see what is where. Colin 2009/5/21 Phoenix Rising <PolarisRising-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > Thanks for your reply Colin. In theory, there should be some way I > can test for the presence or absence of errors on something like: > @report.notifiers.errors.on (something?) > > The thing is, since the post creates a new record for the associated > model, I''m not quite sure how to "access" that. > > On May 21, 1:21 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > You should be able to do an assert_select looking for whatever the <%> > errors_for :maintenance_report %> generates for the error you are > > expecting. Or have I missed a subtlety of your question? > > > > Colin > > > > 2009/5/21 Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > > > > > > > > > > The title of this post may be somewhat misleading; I am indeed UNIT > > > testing this, and it works fine, however I''m also functional testing a > > > POST to an action that is responsible for creating a new ActiveRecord > > > object that''s associated with the "parent" via has_many :through. > > > > > class Report < ActiveRecord::Base > > > # ... > > > has_many :report_notifiers > > > has_many :notifiers, :through => :report_notifiers > > > # ... > > > end > > > > > class ReportNotifier < ActiveRecord::Base > > > # ... > > > belongs_to :report > > > # ... > > > end > > > > > So, creating a new report with a new notifier would be simple: > > > @r = Report.new > > > @r.notifiers << Notifier.create(:label => "something", :email => > > > "f...-+RB1Aph5k6s@public.gmane.org") > > > > > But what happens when a user submits invalid e-mail address? Well, of > > > course, the validation fails. But how do I go about testing for the > > > presence of that validation failure message in my functional test? > > > > > Here''s my existing functional test: > > > -------------------- > > > def test_edit_with_invalid_notifiers > > > m = Factory(:maintenance_report) # I dig factory_girl, great > > > plugin > > > > > post :edit, {:m => m, :id => m.id, :notifiers => [1, 2, 3, '''', 0, > > > ''asdfasf'', "\n\n;'';'''''''';UPDATE notifiers SET label=\"hacked\""], > > > :new_notifiers => "not a valid email!asdfasdfasf afd asdfasf 2 > > > wgw@a asdf...\n\n\n\tsomevalidem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"} > > > > > # ...now what? > > > > > end > > > --------------------- > > > > > As you can see, my test gives the controller action invalid data, > > > which will trigger an error when the validation is attempted. But, > > > given that the Notifier class, which contains the validation, > > > belongs_to the Report class, which is what I''m creating a new one of, > > > how do I test to see if we get any errors? > > > > > i.e. in a template I''d just have: > > > <%= errors_for :maintenance_report %> > > > > > ...which would theoretically contain a bullet point saying something > > > about "invalid e-mail address". But how do I test that? > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phoenix Rising
2009-May-21 16:23 UTC
Re: Functional testing a has_many :through validation
Colin, thanks for your help. You got me thinking in a different direction. I tried what you had there, but noticed that the test didn''t even get to that point. It failed IMMEDIATELY after the original post, so the assertion never fired. Which got me thinking: why not a rescue? begin post :edit, {:m => m, :id => m.id, :notifiers => [1, 2, 3, '''', 0, ''asdfasf'', "\n\n;'';'''''''';UPDATE notifiers SET label=\"hacked\""], :new_notifiers => "not a valid email!asdfasdfasf afd asdfasf 2 wgw@a asdf...\n\n\n\t\tfoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"} rescue ActiveRecord::RecordInvalid => e assert_equal "Validation failed: Email is an invalid e-mail address.", e.message end This works perfectly. Thanks for your help! On May 21, 10:13 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> I think maybe > assert assigns(:report).notifiers.errors.on(:fieldname) > where fieldname is the field that you have added the error on. > Or something like that anyway. > If it doesn''t work you could break into the test code with the ruby debugger > at that point, then you can inspect the variables and see what is where. > > Colin > > 2009/5/21 Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > > > > Thanks for your reply Colin. In theory, there should be some way I > > can test for the presence or absence of errors on something like: > > @report.notifiers.errors.on (something?) > > > The thing is, since the post creates a new record for the associated > > model, I''m not quite sure how to "access" that. > > > On May 21, 1:21 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > You should be able to do an assert_select looking for whatever the <%> > > errors_for :maintenance_report %> generates for the error you are > > > expecting. Or have I missed a subtlety of your question? > > > > Colin > > > > 2009/5/21 Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > > The title of this post may be somewhat misleading; I am indeed UNIT > > > > testing this, and it works fine, however I''m also functional testing a > > > > POST to an action that is responsible for creating a new ActiveRecord > > > > object that''s associated with the "parent" via has_many :through. > > > > > class Report < ActiveRecord::Base > > > > # ... > > > > has_many :report_notifiers > > > > has_many :notifiers, :through => :report_notifiers > > > > # ... > > > > end > > > > > class ReportNotifier < ActiveRecord::Base > > > > # ... > > > > belongs_to :report > > > > # ... > > > > end > > > > > So, creating a new report with a new notifier would be simple: > > > > @r = Report.new > > > > @r.notifiers << Notifier.create(:label => "something", :email => > > > > "f...-+RB1Aph5k6s@public.gmane.org") > > > > > But what happens when a user submits invalid e-mail address? Well, of > > > > course, the validation fails. But how do I go about testing for the > > > > presence of that validation failure message in my functional test? > > > > > Here''s my existing functional test: > > > > -------------------- > > > > def test_edit_with_invalid_notifiers > > > > m = Factory(:maintenance_report) # I dig factory_girl, great > > > > plugin > > > > > post :edit, {:m => m, :id => m.id, :notifiers => [1, 2, 3, '''', 0, > > > > ''asdfasf'', "\n\n;'';'''''''';UPDATE notifiers SET label=\"hacked\""], > > > > :new_notifiers => "not a valid email!asdfasdfasf afd asdfasf 2 > > > > wgw@a asdf...\n\n\n\tsomevalidem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"} > > > > > # ...now what? > > > > > end > > > > --------------------- > > > > > As you can see, my test gives the controller action invalid data, > > > > which will trigger an error when the validation is attempted. But, > > > > given that the Notifier class, which contains the validation, > > > > belongs_to the Report class, which is what I''m creating a new one of, > > > > how do I test to see if we get any errors? > > > > > i.e. in a template I''d just have: > > > > <%= errors_for :maintenance_report %> > > > > > ...which would theoretically contain a bullet point saying something > > > > about "invalid e-mail address". But how do I test that?