当新用户注册时,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/