[rspec-users] RSpec - Testing ActiveRecord addins

James Byrne lists at ruby-forum.com
Fri Jan 30 11:10:10 EST 2009


Pat Nakajima wrote:

> If you want an easy way to test ActiveRecord extensions, check out
> acts_as_fu: http://github.com/nakajima/acts_as_fu. It makes generating
> ActiveRecord models dead simple.
> 

This seems to work very well.  However, I am causing myself a problem 
with the logger and I could use some clarification on what is happening 
so that I can fix it.

I created a custom logger to format the log output into syslog style. 
So, in environment.rb I have this:

  # Customize logger - 'require "syslog_formatter"'
  config.logger = RAILS_DEFAULT_LOGGER = Logger.new(config.log_path)
  config.logger.formatter = SyslogFormatter.new
  config.logger.level     = Logger::INFO # DEBUG, WARN, ERROR, FATAL

and lib/syslog_formatter.rb has this:

# Configure custom logger used in all environments

class SyslogFormatter
  def call(level, time, program, message)
    l_time    = time.strftime("%b %d %H:%M:%S")
    l_process = "rails[#{$PID}]"
    l_host    = Socket.gethostname.split('.')[0]
    l_user    = @current_user if defined?(@current_user)
    l_text    = (String === message ?  message : 
message.inspect).gsub(/\n/, '').strip
    "#{l_time} #{l_host} #{l_process} #{l_text} #{l_user}\n"
  end
end

and when I run rake spec I see this:

NameError in 'Role should create a new instance given valid attributes'
undefined local variable or method `logger' for 
#<Spec::Rails::Example::ModelExampleGroup::Subclass_4:0x2ad081fa0998>
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:471:in 
`method_missing'
/home/byrnejb/Software/Development/Projects/proforma.git/spec/spec_helper.rb:25:

Which is explicitly related to this code in spec_helper.rb:

# Custom Logger

  config.before(:each) do
    full_example_description = "#{self.class.description} 
#{@method_name}"
    logger.info(
      "\n\n#{full_example_description}\n#{'-' *
          (full_example_description.length)}")
  end

Do I just comment this out or is there something else I should do, short 
of disabling my own custom logger, to get this to work?



-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list