使用rspec调用确保日志记录器



我试图在特定条件下测试我的rails应用程序中的控制器,应该引发错误并记录它。我得到了raise测试工作很好,但我想确保日志记录器被调用,到目前为止,我尝试了这个,但它不工作

  it 'logs the error' do
    get 'edit', {:id => 'banana'}
    Logger.any_instance.expects(:error)
  end

我用的是Mocha

对于rails 4,正确的方法是:

expect(Rails.logger).to receive(:error).with("your error message")

我很清楚我想要什么,例如:

 Rails.logger.expects(:error).with('this error message')

是如何使用mocha来断言一个特定的错误消息正在被记录。

使用rspec可以这样写:

Rails.logger.should_receive(:error).with("your error message")

最新更新