ruby-on-rails - Rails5 集成测试 assert_equal 与闪存消息
问题描述
在 Rails 5,2 中进行了一个非常简单的系统测试:
require 'test_helper'
class AdminSignsInTest < ActionDispatch::SystemTestCase
driven_by :selenium, using: :firefox
test 'admin can sign in' do
@user = users(:admin)
visit new_user_session_path
fill_in 'user_username', with: @user.username
fill_in 'user_password', with: @user.encrypted_password
click_button I18n.t("devise.login")
assert_equal I18n.t('devise.failure.invalid'), flash[:alert]
end
end
返回错误:
NoMethodError: undefined method `flash' for nil:NilClass
根据 Rails 文档http://guides.rubyonrails.org/testing.html#testing-flash-notices这个语法应该有效,例如:
assert_equal 'Article was successfully created.', flash[:notice]
解决方案
签入@response
或response
对象必须有警报消息。你可以试试。
推荐阅读
- javascript - UseEffect() 不断给出重定向错误
- c# - 从另一个线程更新 Blazor 中的项目列表
- java - 想在变量中分配 12 位长数字
- linux - 需要 bash 脚本的指导来检查某个目录中的日志文件中的某个字符串
- java - Java HttpsURLConnection 和 Basic auth 对于中间人攻击是否足够安全?
- graphviz - 如何编辑 grphviz .dot 文件使其看起来更体面?
- java - 从 Java 源代码生成 AST 报告而不实际运行它
- python - Pandas - 如何使用来自 DateTimeIndex 列表的索引添加新的空行?
- apache - htaccess 中的 RewriteRules 用于干净的 URL 解析并从完整 URL 重定向到干净的 URL
- javascript - HTML/CSS 按钮在移动设备上表现怪异(Chrome/Android)