工厂女孩中非活动记录关联的问题
我在测试时才开始使用工厂女孩来更换灯具。 我正在开发一个 Twitter 客户端,我正在尝试使用 Factory Girl 来创建用于测试的 Twitter 对象。 当我单独创建它们时,就很好了。 但是,如果我尝试将它们关联起来,则会收到以下错误。
Factory.define :status, :class => Twitter::Status, :default_strategy => :build do |t|
t.text 'Test Twitter Status message'
t.association :user, :factory => :twitter_user #this line causes the problems
end
Factory.define :twitter_user, :class => Twitter::User, :default_strategy => :stub do |u|
u.profile_image_url "#{RAILS_ROOT}/public/images/rails.png"
end
t.association :user, :factory => :twitter_user
会导致问题,因为当它存在时会抛出此异常。 有没有什么办法解决这一问题? 或者 Factory Girl 是专为 ActiveRecord 对象设计的吗? 谢谢
NoMethodError: undefined method `save!' for #<Twitter::User:0x4af3de46>
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/proxy/create.rb:5:in `result'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:293:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:237:in `create'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/proxy/build.rb:17:in `associate'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/attribute/association.rb:13:in `add_to'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:290:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:288:in `each'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:288:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:217:in `build'
test/functional/tweet_feeds_controller_test.rb:12:in `test_Display_friends_timeline_for_the_'amber'_user'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `run_with_callbacks_and_mocha'
I just started to use factory girl to replace fixtures when I am testing. I am working on a twitter client and I am trying to use factory girl to create the twitter objects for testing. When I create them individually it is fine. But, if I try to associate them I get the error below.
Factory.define :status, :class => Twitter::Status, :default_strategy => :build do |t|
t.text 'Test Twitter Status message'
t.association :user, :factory => :twitter_user #this line causes the problems
end
Factory.define :twitter_user, :class => Twitter::User, :default_strategy => :stub do |u|
u.profile_image_url "#{RAILS_ROOT}/public/images/rails.png"
end
The t.association :user, :factory => :twitter_user
causes the problems because when it is there this exception is thrown. Is there anyway to fix this? Or is factory girl just designed for activerecord objects? Thanks
NoMethodError: undefined method `save!' for #<Twitter::User:0x4af3de46>
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/proxy/create.rb:5:in `result'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:293:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:237:in `create'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/proxy/build.rb:17:in `associate'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/attribute/association.rb:13:in `add_to'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:290:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:288:in `each'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:288:in `run'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:217:in `build'
test/functional/tweet_feeds_controller_test.rb:12:in `test_Display_friends_timeline_for_the_'amber'_user'
/usr/local/share/jruby-1.1.6/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `run_with_callbacks_and_mocha'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定 Factory Girl 仅适用于 ActiveRecord 对象,如果您可能使用 rSpec (如果我的语法正确),您应该能够模拟并存根 Twitter::User 对象而不是使用 Factory:
然后将其附加到需要它的对象上。
这可能不太正确,但这是我要开始的道路。
I'm pretty sure that factory girl is just for ActiveRecord objects, you should just be able to mock up and stub a Twitter::User object rather than use a Factory, if you were using rSpec maybe (if my syntax is right):
and then attach that to your object that needs it.
This might not be quite right, but it's the path I would start down.