1

Do you know the syntax for testing a flash message using system tests?

This is how I'm currently writing it...

test 'New User signup' do
  # ...
  click_button 'Create User'
  assert_equal 'User was successfully created.', flash[:notice]
end

The failing test screenshot shows the flash is there but the test fails with

NoMethodError: NoMethodError: undefined method `flash' for nil:NilClass

notapatch
  • 6,569
  • 6
  • 41
  • 45
DaveWoodall.com
  • 727
  • 6
  • 22

1 Answers1

2

I ended up changing the assertion from:

assert_equal 'User was successfully created.', flash[:notice]

to:

assert_selector "#notice", text: 'User was successfully created.'

which seemed to do the trick.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
DaveWoodall.com
  • 727
  • 6
  • 22
  • here are other ways to test flash messages with rspec and shoulda (quite old but still valid): http://stackoverflow.com/questions/24919976/rspec-3-how-to-test-flash-messages – StandardNerd May 19 '17 at 21:35
  • @StandardNerd not really relevant if you are doing Rails 5 integration tests - not controller tests. – max May 19 '17 at 21:43
  • 2
    What you're doing is the "right" way. Instead of poking in the internals you are actually testing page as the user sees it. – max May 19 '17 at 21:45