如何rspec模拟ruby rails logger类

嗨我,并尝试rspec模拟以下类:

class Person def initialize(personid) Rails.logger.debug "Creating person with id #{personid}" end end 

使用这个:

 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 

并收到此消息:

RSpec :: Mocks :: MockExpectationError 🙁 Double“Rails.logger”)。debug(any args)
预计:1次
收到:0次

任何帮助都会很棒!

我会做:

 Rails.stub_chain(:logger, :debug).and_return(logger_mock) 

不要忘记在测试结束时取消存根:

 Rails.unstub(:logger) 

存根不起作用,因为这些存根没有链接到实际代码。 它应该是这样的:

 require 'spec_helper' describe Person do describe "#initialize" do let(:logger_mock) { double("Rails.logger").as_null_object } it "logs a message" do Rails.stub(:logger).and_return(logger_mock) logger_mock.should_receive(:debug) Person.new "dummy" end end end 

对于OP:如果您只想设置日志记录的期望,则根本不需要存根整个记录器类。 你可以这样做

 Rails.logger.should_receive(:debug) 

额外:如果您只想存根,那么不会发生日志记录,请执行以下操作:

 Rails.logger.stub(:add){ true }