Rspec 3 如何测试 Flash 消息 [英] Rspec 3 how to test flash messages

查看:29
本文介绍了Rspec 3 如何测试 Flash 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 rspec 测试控制器的动作和 flash 消息的存在.

I want to test controller's action and flash messages presence with rspec.

行动:

def create
  user = Users::User.find_by_email(params[:email])
  if user
    user.send_reset_password_instructions
    flash[:success] = "Reset password instructions have been sent to #{user.email}."
  else
    flash[:alert] = "Can't find user with this email: #{params[:email]}"
  end

  redirect_to root_path
end

规格:

describe "#create" do
  it "sends reset password instructions if user exists" do
    post :create, email: "email@example.com"      
    expect(response).to redirect_to(root_path)
    expect(flash[:success]).to be_present
  end
...

但是我有一个错误:

Failure/Error: expect(flash[:success]).to be_present
   expected `nil.present?` to return true, got false

推荐答案

您正在测试 flash[:success] 的存在,但在您的控制器中您使用的是 flash[:通知]

You are testing for the presence of flash[:success], but in your controller you are using flash[:notice]

这篇关于Rspec 3 如何测试 Flash 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆