代码之家  ›  专栏  ›  技术社区  ›  Coderama

工厂女孩有很多:通过验证

  •  1
  • Coderama  · 技术社区  · 15 年前

    class Activity < ActiveRecord::Base
      has_many  :clientships, :dependent => :destroy
      has_many  :clients, :through => :clientships
    end
    
    class Clientship < ActiveRecord::Base
      belongs_to  :client
      belongs_to  :activity
    
      validates_presence_of :client_id
      validates_presence_of :activity_id, :unless => :new_record?
    end
    
    class Client < ActiveRecord::Base
      has_many  :clientships
      has_many  :activities, :through => :clientships
    end
    

    我无法创建:活动工厂,因为我得到验证错误“ ".

    我的工厂是这样的:

    Factory.define :activity do |a|
      a.association :staff, :factory => :user
      a.clientships { |cs| [cs.association :clientship] }
    end
    
    Factory.define :clientship do |cs|
      cs.association(:client)
    end
    
    Factory.define :client do |c|
      c.first_name {Factory.next(:name)}
      c.last_name {Factory.next(:name)}
    end
    

    @activity = Factory(:activity)

    请帮帮我!

    1 回复  |  直到 15 年前
        1
  •  3
  •   Fran    15 年前

    在这种情况下,我经常做的是:

    Factory.define :activity do |a|
      #whatever attributes
    end
    
    Factory.define :clientship do |cs|
      cs.association(:client)
      cs.association(:activity)
    end
    
    Factory.define :client do |c|
      c.first_name {Factory.next(:name)}
      c.last_name {Factory.next(:name)}
    end
    

    所以在我的测试/规格中

    Factory :clientship
    

    belongs_to 因为最后它对我来说问题不大。