Question

I am trying to test a controller in my rails app under a specific condition that should raise an error and log it. I got the raise test working great but I would like to make sure the logger gets called, so far I tried this, but it is not working

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

ps i am using Mocha

Was it helpful?

Solution 2

I figured out exactly what I wanted, for example:

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

Is how you can use mocha to assert that a specific error message is being logged.

OTHER TIPS

With rails 4, the right way to do it is the following

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

Using rspec you would write:

Rails.logger.should_receive(:error).with("your error message")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top