ruby-on-rails - Rails 4 中的 Rspec flash 集成测试错误 : NameError: undefined local variable or method `flash' for #

标签 ruby-on-rails flash rspec

当新用户注册时,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 => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a882a8eca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>')
    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 方法:

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

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

最佳答案

正如您所写,您正在从 Controller 测试 create 方法。这就是为什么您应该在 Controller 规范中测试闪存消息 - 那么它应该可以工作。

关于ruby-on-rails - Rails 4 中的 Rspec flash 集成测试错误 : NameError: undefined local variable or method `flash' for #,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30144272/

相关文章:

mysql - Ruby on Rails 无法连接到 mysql 数据库

flash - 如何缩放填充而不是Actionscript中的笔划?

ruby - Webmock 没有正确注册我的请求 stub

ruby-on-rails - 使用 RSpec 测试 URL(正则表达式)验证

ruby-on-rails - 如何在没有Web服务器的情况下在WWW上发布我的应用程序?

javascript - Rails 500 内部服务器错误

mysql - rails 3 中的关联关联 1 个调查给 1 个用户

flash - 谷歌是否计算来自 Flash 的反向链接?

html - 如何强制 Flash 崩溃以进行测试?

ruby - 使用 headless_chrome 模拟设备