工厂女孩的非activerecord关联问题

时间:2009-02-27 14:57:20

标签: ruby-on-rails ruby factory factory-bot

我在测试时刚刚开始使用工厂女孩来更换灯具。我正在使用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导致问题,因为当它出现时会抛出此异常。有没有什么办法解决这一问题?或者工厂女孩只是为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'

1 个答案:

答案 0 :(得分:1)

我很确定工厂女孩只是用于ActiveRecord对象,你应该能够模拟和存根Twitter :: User对象,而不是使用工厂,如果你使用rSpec可能(如果我的语法是右):

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

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

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