john
2008-Jun-12 23:03 UTC
[rspec-users] Does anyone know how to mock the Rails Logger then set expectations with should_receive?
Hey Guys, I''m trying to mock the Rails Logger for the following code: ... rescue TimeoutError => error $logger.error("#{self.name} Timeout for #{path}: #{error}") and return rescue SocketError => error $logger.error("#{self.name} SocketError for #{path}: #{error}") and return rescue StandardError => error $logger.error("#{self.name} Error for #{path}: #{error}") and return end ... my failed attempt to spec: logger = mock_model(Logger) logger.stub(:error) logger.should_receive(:error).with(:any_args) Do you know of any solution for mocking this? Also, do you think the Rails Logger is worth mocking? Thanks very much in Advance, -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080612/841a230f/attachment-0001.html>
Pat Maddox
2008-Jun-12 23:30 UTC
[rspec-users] Does anyone know how to mock the Rails Logger then set expectations with should_receive?
On Thu, Jun 12, 2008 at 4:03 PM, john <eagerwombat at gmail.com> wrote:> Hey Guys, > > I''m trying to mock the Rails Logger for the following code: > > > ... > rescue TimeoutError => error > $logger.error("#{self.name} Timeout for #{path}: #{error}") and return > rescue SocketError => error > $logger.error("#{self.name} SocketError for #{path}: #{error}") and > return > rescue StandardError => error > $logger.error("#{self.name} Error for #{path}: #{error}") and return > end > ... > > > > my failed attempt to spec: > > logger = mock_model(Logger) > logger.stub(:error) > logger.should_receive(:error).with(:any_args)You never set the $logger var to your new logger. So it wouldn''t do anything.> Do you know of any solution for mocking this?You could consider partially stubbing the rails logger. $logger.should_receive(:error)> Also, do you think the Rails Logger is worth mocking?I would say no. In particular, sometimes logging is a domain requirement. If that''s the case, you want to separate that kind of logging from all the log info that Rails generates. Read http://www.mockobjects.com/2007/04/test-smell-logging-is-also-feature.html to get some info on mocking loggers. Pat