Using rspec the ensure logger is called
-
13-01-2021 - |
Frage
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
Lösung 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.
Andere Tipps
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")
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow