jeffhale
jeffhale

Reputation: 4032

Rspec flash integration test error in Rails 4: NameError: undefined local variable or method `flash' for #

Flash success message displays fine when a new user signs up. Rspec integration test fails with:

user is redirected upon successful signup displays a flash message

Failure/Error: expect(flash[:success]).to be_present
 NameError:
   undefined local variable or method `flash' for #<RSpec::ExampleGroups::UserIsRedirectedUponSuccessfulSignup:0x007fb447b8bd40>
 # ./spec/features/join_feature_spec.rb:21:in `block (2 levels) in <top (required)>'

Spec file contents:

context "user is redirected upon successful signup", :type => :feature do
 before :each do
  visit '/join'
    fill_in('Name', :with => 'Meg')
    fill_in('Email', :with => '[email protected]')
    fill_in('Password', :with => 'jjjjcccc')
    fill_in('Password confirmation', :with => 'jjjjcccc')
    click_button('Join')
 end

 it "renders user page after successful signup" do
  expect(page).to have_content('Meg') 
 end

 it "says Hola on the page" do
  expect(page).to have_content('Hola') 
 end

 it "displays a flash message" do
  expect(flash[:success]).to be_present
 end
end

Controller method:

def create
  @user = User.new(user_params)
  if @user.save
    flash[:success] = "Welcome to the ACLfix community!"
    redirect_to @user
  else
    render 'new'
  end
end

Can't figure out why the test is failing - looked a bunch. Any help much appreciated.

Upvotes: 2

Views: 1319

Answers (1)

pmichna
pmichna

Reputation: 4888

As you have written, you are testing the create method from the controller. That's why you should test flash messages in a controller spec - it should work then.

Upvotes: 2

Related Questions