我想用 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[:notice]
shoulda gem 提供了测试 Flash 消息的最佳方法。
以下是三个例子:
expect(controller).to set_flash
expect(controller).to set_flash[:success]
expect(controller).to set_flash.now[:alert].to(/are not valid/)
如果您对 Flash 消息的内容更感兴趣,可以使用:
expect(flash[:success]).to match(/Reset password instructions have been sent to .*/)
或者
expect(flash[:alert]).to match(/Can't find user with this email: .*/)
我建议不要检查特定消息,除非该消息很关键和/或它不经常更改。
有:gem 'shoulda-matchers', '~> 3.1'
.now
应直接在 set_flash
上调用。
不再允许将 set_flash
与 now
限定符一起使用并在其他限定符之后指定 now
。
您需要在 set_flash
之后立即使用 now
。例如:
# Valid
should set_flash.now[:foo]
should set_flash.now[:foo].to('bar')
# Invalid
should set_flash[:foo].now
should set_flash[:foo].to('bar').now
另一种方法是忽略控制器具有闪存消息的事实并改为编写集成测试。这样,一旦您决定使用 JavaScript 或其他方式显示该消息,您就无需更改测试的机会增加了。
另请参阅https://stackoverflow.com/a/13897912/2987689
不定期副业成功案例分享
expect(flash[:notice])
更改为expect(flash[:alert])
,如果测试通过,则可能只是测试电子邮件不存在。