Rails 4中的Rspec flash集成测试错误:NameError:未定义的局部变量或方法`flash'for#

时间:2022-10-08 23:53:45

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

当新用户注册时,Flash成功消息显示正常。 Rspec集成测试失败:

user is redirected upon successful signup displays a flash message

成功注册后,用户将被重定向,并显示一条Flash消息

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 => 'j@j.com')
    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.

无法弄清楚为什么测试失败 - 看起来很多。任何帮助非常感谢。

1 个解决方案

#1


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.

正如您所写,您正在从控制器测试create方法。这就是为什么你应该在控制器规范中测试flash消息 - 它应该工作。

#1


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.

正如您所写,您正在从控制器测试create方法。这就是为什么你应该在控制器规范中测试flash消息 - 它应该工作。