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 =>
“[email protected]”)
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\[email protected]”}
# ...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?