rails杂记

[color=red][align=center][b]=======Rails2=======[/b][/align][/color]

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

解决办法:在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。搜索到这个帖子:[url]http://groups.google.com/group/factory_girl/browse_thread/thread/8add0361e717c240[/url]
由于访问google group需要特殊技能,所以copy一份过来:
[quote="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? [/quote]
由于是上班时间,没有空多作研究,暂时先把代码注释掉一些:
#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

使用正常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值