How do I mute a flash in the RSpec spec?

In my opinion, I have hidden_field_tag

which value is flash

set in the controller. In other words, the flow looks like this:

Controller:

def home
    flash[:id] = 123
end

      

View:

<% form_tag(new_invitee_path) %>
  <%= hidden_field_tag :referer, flash[:id] %>
<% end %>

      

The parameters presented in new_invitee_path :

{ "referer" => "123" }

      

I can confirm that with manual testing this works as expected, but I cannot figure out how to stub it appropriately.

In my test I:

before do
  #set flash
  visit '/home'
  fill_in "rest_of_form"
  click_button "submit_form
end

      

The following are the things I tried to do for set flash

and the error messages I receive:

flash[:id] = 123 
# OR
flash.now[:id] = 123 
# both render error: undefined local variable or method `flash' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fc1040f7d60>

# Have also tried a tactic found online to set flash for response object like this:
visit '/home'
response.flash[:id] = 123
# OR
response.flash.now[:id] = 123
# both render error: undefined local variable or method `response' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe118a38490>

#Have read online that it a problem with the flash being sweeped, so I tried to stub out the sweep, but am unclear how to set the anonymous controller or whatever correctly
controller.instance_eval{flash.stub!(:sweep)}
flash[:id] = 123 
# OR
flash.now[:id] = 123 
# renders error: undefined local variable or method `flash' for nil:NilClass

      

+3


source to share


2 answers


Your spec is a function spec, so the spec framework doesn't have access to things like flash. Do not try to operate the flash directly. Instead, ideally, check that the custom view of the application looks and / or behaves as it should if the flash value is set as it should. I wouldn't just check that the hidden field is present on the form; I would experience it has the effect it should after the form is submitted. That all features of the specification: testing that the application works as a whole from the user's point of view.



If the flash value is never used in the user interface, it is just logged or stored in the database, it would be good to check that the log string or model object has a value that is stored in flash. (The user here is the admin who will look at the log or whatever later.) But if the flash affects the UI, testing is preferred.

+4


source


I think this works pretty well:



YourController.any_instance.stub(:flash) { {some: "thing" }}

      

+2


source







All Articles