should_set_the_flash_to doesn't do quite what I'd like. I want to: a) specify which level of flash should be set (ie make sure it's a notice and not an error) and b) not care exactly what notice has been set.
IMO tying your tests down too hard to ephemeral strings is annoying... what is somebody changed the wording from "user created" to "thanks for signing up"? You have to change your test cases for that? :P So, being able to just enter "anything" should be possible.
Note - it piggybacks on the existing flash shoulda macro, and thus allows you to use it as before (pass 'nil' for level if you want - but it's better just to use the original if you need it.
So, here's my updated code. Dump it into the bottom of your test_helper file.
class ActionController::TestCase # Make sure that a message has been set at the given flash level # you can test that a notice has been posted, but no error thus: # should_set_flash :notice # should_not_set_flash :error def self.should_set_the_flash(level = nil, val = :any) val = /.*/i if val == :any return should_set_the_flash_to val unless level if val.blank? should "have nothing in the #{level} flash" do assert flash[level].blank?, "but had: #{flash[level].inspect}" end else should "have something in the #{level} flash" do assert !flash[level].blank?, "No value set of given flash level: #{level}" end should "have #{val.inspect} in the #{level} flash" do assert_contains flash[level], val, ", Flash: #{flash[level].inspect}" end end end # convenience method for making sure nothing has been set for the given # flash level def self.should_not_set_the_flash(level = nil) should_set_the_flash level, nil end end
[Edit: 22-Apr-2009] : ADDED a convenience method for "not_set" and updated "set" to take advantage of this. Also updated comments to include an example of use
1 comment:
I prefer assert_match rather than assert_contains to match the flash content IMHO
http://gist.github.com/206977
Post a Comment