Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I log to test.log with rspec?

Here's a simple spec just to test this:

require 'spec_helper'
describe A_controller do
    before do
         @cat_noise = "Meow"
    end
    it "should do nothing because it's empty and I created it just to test logger!" do
       logger.info "---------- Here comes a cat noise -----------"
       logger.info @cat_noise
       logger.info "-----------There goes a cat noise -----------"
    end
end

And here's my log level configuration for the test environment:

  config.log_level = :info

However, I'm getting nothing to do with cat noises in my test.log file. Should I be? Would it appear in test.log? It would be good to double check factories and the like...

like image 950
Starkers Avatar asked Dec 07 '13 00:12

Starkers


Video Answer


1 Answers

I'm not sure where that logger variable comes from. There is no logger available inside an RSpec block. The code should crash with

NameError:
  undefined local variable or method `logger' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007ffa3208b478>

In order to write to the Rails log, you should use

Rails.logger

Thus

require 'spec_helper'
describe A_controller do
    before do
        @cat_noise = "Meow"
    end

    it "should do nothing because it's empty and I created it just to test logger!" do
       Rails.logger.info "---------- Here comes a cat noise -----------"
       Rails.logger.info @cat_noise
       Rails.logger.info "-----------There goes a cat noise -----------"
    end
end
like image 164
Simone Carletti Avatar answered Sep 30 '22 04:09

Simone Carletti