我想用rspec測試控制器的動作和flash消息存在。Rspec 3如何測試Flash消息
行動:
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 protected]"
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
哦,對不起,我只是在這裏滑了。 與閃存相同的錯誤[:notice] – MikeAndr
在這種情況下,問題可能在於您的控制器代碼和/或測試數據。嘗試將'expect(flash [:notice])''更改爲'expect(flash [:alert])',如果測試通過,那麼可能只是測試郵件不存在。 – rabusmar