【问题标题】:How to make has_many :through association with fixtures?如何制作 has_many :通过与灯具的关联?
【发布时间】:2011-09-08 18:28:36
【问题描述】:

我不能使用factory_girl,因为我正在测试太阳黑子并且需要真正的数据库。

编辑:不。它可以与太阳黑子一起使用。我错了。

我如何构建 has_many :through(a.k.a many-to-many) 关联?

我用谷歌搜索得到invalid solution

编辑

最后我使用了 factory_girl。我google-copy-paste一个sn-p:

factory :tagging do 
    question { |a| a.association(:question) } 
    tag { |a| a.association(:tag) } 
end

(通过taggings询问has_many标签,反之亦然)

效果很好。但它是什么? factory_girl 的自述文件并不是指这种语法。 有人可以解释一下吗?

【问题讨论】:

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


    【解决方案1】:

    你可以找到factory_girl的官方文档,很全,here

    Here 是一篇不错的(较短的)博文,解释了 factory_girl 2(与 factory-girl 1 进行比较)。

    更新:

    稍微解释一下代码:

     factory :tagging do
       association :tag
     end
    

    将查找名为 :tag 的工厂并构造该对象,然后将其链接到对象 :tagging 中的关联 tag(例如 belongs_to)。

    请注意:这是默认工厂。如果你想让taggings 分享tag,你需要做类似的事情

    @tag = Factory(:tag)
    @tagging_1 = Factory(:tagging, :tag => @tag)
    @tagging_2 = Factory(:tagging, :tag => @tag)
    

    希望这会有所帮助。

    【讨论】:

    • association 返回什么?两个文件都没有说。
    • 您的解决方案不等同于我的解决方案。 association :tag 将调用 FactoryGirl.create(:tag) 或其他东西。所以如果两个标签属于同一个标签,它将被创建两次。我的(谷歌搜索)解决方案避免了这种情况。
    • 恕我直言解决方案是相同的。但实际上关联的tag 将被创建两次。如果您将显式 :tag 参数传递给工厂,它将使用该参数。请参阅我的更新答案。
    【解决方案2】:

    如果它是一个经典的 has_and_belongs_to_many 关联,在关联模型中没有其他信息,我认为约定允许您这样编写您的固定装置:

    #users.yml
    john:
      first_name: John
      last_name: Doe
      hobbies: [swim, play_tennis]
    
    #hobbies.yml
    swim:
      name: Swim
    
    play_tennis:
      name: Play Tennis
    

    但我不完全确定!

    【讨论】:

      【解决方案3】:

      我在通过哈希 merge 测试 has_many :through 时使用了固定装置

      # posts.yml
      one:
        title: "Railscasts"
        url: "http://railscasts.com/"
        description: "Ruby on Rails screencasts"
      
      # categories.yml
      one:
        name: "magazine"
      two:
        name: "tutorial"
      three:
        name: "news"
      four:
        name: "Ruby"
      
      # posts_controller_test.rb
      def test_post_create
        assert_difference 'Post.count' do
          post :create, post: posts(:one).attributes
           .merge(categories: [categories(:two), categories(:four)])
        end
      end
      

      当添加另一个夹具文件后,并尝试了它没有工作

      # post_categories.yml
      one:
        post: one
        category: two
      two:
        post: one
        category: four
      
      def test_post_create
        assert_difference 'Post.count' do
          post :create, post: posts(:one)
        end
      end
      
      puts posts(:one).attributes
      # {"id"=>980190962, "url"=>"http://railscasts.com/", "title"=>"Railscasts", "description"=>"Ruby on Rails screencasts", "created_at"=>Thu, 14 May 2015 18:27:20 UTC +00:00, "updated_at"=>Thu, 14 May 2015 18:27:20 UTC +00:00}
      
      puts posts(:one).attributes
            .merge(categories: [categories(:two), categories(:four)])
      # {"id"=>980190962, "url"=>"http://railscasts.com/", "title"=>"Railscasts", "description"=>"Ruby on Rails screencasts", "created_at"=>Thu, 14 May 2015 18:30:23 UTC +00:00, "updated_at"=>Thu, 14 May 2015 18:30:23 UTC +00:00, "category_ids"=>[980190962, 1018350795]}
      

      【讨论】:

        猜你喜欢
        • 2015-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-09
        • 1970-01-01
        • 2013-06-27
        • 2012-07-29
        相关资源
        最近更新 更多