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

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

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

用户在成功注册后被重定向并显示一条闪现消息

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)>'

规格文件内容:

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

控制器方法:

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

无法弄清楚测试失败的原因 - 看了一堆。非常感谢任何帮助。

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