gpt4 book ai didi

ruby-on-rails - 工厂女孩中非事件记录协会的问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:11 26 4
gpt4 key购买 nike

我刚开始在测试的时候用factory girl来替换fixtures。我正在开发一个 Twitter 客户端,我正在尝试使用工厂女孩来创建用于测试的 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'

最佳答案

我很确定 factory girl 只适用于 ActiveRecord 对象,你应该能够模拟和 stub Twitter::User 对象而不是使用工厂,如果你使用 rSpec 也许(如果我的语法是右):

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

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

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

关于ruby-on-rails - 工厂女孩中非事件记录协会的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/595103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com