工厂女孩中非活动记录关联的问题

发布于 2024-07-14 03:15:23 字数 2221 浏览 9 评论 0原文

我在测试时才开始使用工厂女孩来更换灯具。 我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-07-21 03:15:23

我很确定 Factory Girl 仅适用于 ActiveRecord 对象,如果您可能使用 rSpec (如果我的语法正确),您应该能够模拟并存根 Twitter::User 对象而不是使用 Factory:

@twitter_user = mock(Twitter::User, :profile_image =>"#{RAILS_ROOT}/public/images/rails.png")

然后将其附加到需要它的对象上。

这可能不太正确,但这是我要开始的道路。

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):

@twitter_user = mock(Twitter::User, :profile_image =>"#{RAILS_ROOT}/public/images/rails.png")

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文