Ben Downey
Ben Downey

Reputation: 2665

Testing module without Rails

I've got some Rspec tests that I'm using to test a module. The module is used by a number of models in my Rails app. In order to keep testing time down to a minimum, I want to avoid loading Rails when testing my module.

The problem is that there are a few methods that contain rescues and those rescues mention the Rails debugger. When Rspec sees the capital 'r' in Rails, it has a bad time because it considers it an uninitialized constant.

I was hoping that if I wrapped up the logging info into its own method, I could avoid the problem. Here's what I came up with. But if I go down this road, I would need to stub either the rescue code or or the log_info method. Is that right? How would I do that? And which one should I stub?

def download_robots_file(page)
  file = Net::HTTP.get(URI("#{page}robots.txt"))
rescue StandardError => ex
  log_info('robot_file', ex)
end
....
private

def log_info(problem, exception_issue)
  Rails.logger.debug("Couldn't download #{problem}: " + exception_issue.inspect)

end

Upvotes: 0

Views: 148

Answers (1)

Haris Krajina
Haris Krajina

Reputation: 15286

You can add to stub chain method that you want to stub.

Rails.stub_chain(:logger).and_return(mocked_logger_instance)

Un-stub in the end with:

Rails.unstub(:logger)

All credits go to mister on following link How to rspec mock ruby rails logger class

Upvotes: 2

Related Questions