How do you test flash[:danger] an other flash messages in Rails?

603 Views Asked by At

I'm writing an integration test for a controller that renders a page and drops a flash[:danger] message if the update action is not performed for X reason.

This is the test I came up with:

  test "#update should render correct page if not successful"  do
    subscription_cart = subscription_carts(:active_one)
    subscription_cart.stubs(:update).returns(false)
    assert_equal flash[:danger], 'Something went wrong!'
  end

I'm trying to test against that flash message but no matter how I write that last line I get an error:

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

I've also tried with different assert syntaxes:

assert flash[:danger], 'Something went wrong!'
assert_equal 'Something went wrong!', flash[:danger]

What is the correct way?

0

There are 0 best solutions below