首页 > 解决方案 > 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]

标签: ruby-on-railsintegration-testing

解决方案


签入@responseresponse对象必须有警报消息。你可以试试。


推荐阅读