rails杂记

=======Rails2=======


用RSpec测试Authlogic的Session时,调用Session.new出现如下错误:
You must activate the Authlogic::Session::Base.controller with a controller object before creating objects

解决办法:在spec_helper.rb中添加一行代码:
Spec::Runner.configure do |config|
  #......
  config.include(Authlogic::TestCase)
end

然后在Session的测试中加一句:
before :each do
  #....
  activate_authlogic
end


===================================

RubyToolBox上看到各种Rails Fixture Replacement工具中,FactoryGirl排名第一。但是居然有这样一个问题:无论我在spec/下建立一个factories.rb文件或者是在spec/factories/下建立一个*.rb文件,FactoryGirl都无法加载到,最后看源代:
if defined? Rails.configuration
  Rails.configuration.after_initialize do
    Factory.definition_file_paths = [
      File.join(RAILS_ROOT, 'test', 'factories'),
      File.join(RAILS_ROOT, 'spec', 'factories')
    ]
    Factory.find_definitions
  end
else
  Factory.find_definitions
end

发现这个after_initialize方法中的block不会执行,估计是测试环境不会触发这个after_initialize。搜索到这个帖子: http://groups.google.com/group/factory_girl/browse_thread/thread/8add0361e717c240
由于访问google group需要特殊技能,所以copy一份过来:
ook?ook! 写道
Just try: it doesn't, but thank you for the idea.

I'm reading the lib bootstrapper, and it looks like Factory add its
definition file only when Rails is loaded. Debugging the thing, I'm
realizing that Rails.configuration is not yet instance, meaning that I'm not
requiring FG where it expect it.

Trying to initiate FG in environments/test lead to correct beahvior, but why
preventing FG to load factories if not in Rails? It's not a big deal, but I
think FG should load its definition path even if not in a standard Rails
way… I think I'll propose a patch where FG tolerate the fact FG is not
require in environnment.

RFC?

由于是上班时间,没有空多作研究,暂时先把代码注释掉一些:
#if defined? Rails.configuration
#  Rails.configuration.after_initialize do
    Factory.definition_file_paths = [
      File.join(RAILS_ROOT, 'test', 'factories'),
      File.join(RAILS_ROOT, 'spec', 'factories')
    ]
    Factory.find_definitions
#  end
#else
#  Factory.find_definitions
#end

使用正常。

你可能感兴趣的:(thread,Google,Ruby,Rails,rspec)