Hi I and trying to rspec mock the following class:
class Person
def initialize(personid)
Rails.logger.debug "Creating person with id #{personid}"
end
end
Using this:
require 'spec_helper'
describe Person do
describe "#initialize" do
let(:rails_mock) { double("Rails").as_null_object }
let(:logger_mock) { double("Rails.logger").as_null_object }
it "logs a message" do
rails_mock.stub(:logger).and_return(logger_mock)
logger_mock.should_receive(:debug)
Person.new "dummy"
end
end
end
and getting this message:
RSpec::Mocks::MockExpectationError: (Double "Rails.logger").debug(any args)
expected: 1 time
received: 0 times
Any help would be great!
I'd do:
Rails.stub_chain(:logger, :debug).and_return(logger_mock)
Don't forget do unstub at the end of your test:
Rails.unstub(:logger)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With