繁体   English   中英

如何测试has_many:through关联,该关联在与Factory Girl in Rails的关联上也具有validates_presence_of验证?

[英]How do I test a has_many :through association that also has a validates_presence_of validation on the association with Factory Girl in Rails?

我正在尝试使用Factory Girl测试模型,该模型在has_many:through关联上具有validates_presence_of。 尽管该表格可以正常工作,并且我可以编写一个手动填写该表格的测试,但是我无法获得有效的工厂。 该测试不断失败:

it "has a valid work factory" do
    work = FactoryGirl.build(:work)
    work.should be_valid
end

这是错误消息:

Work has a valid work factory
     Failure/Error: work.should be_valid
       expected #<Work id: nil, name: "Client Website", date: "2013-06-03 16:13:08", client: #<Client id: 1, name: "Client Number 4", client_code: "CN4", created_at: "2013-06-04 16:13:12", updated_at: "2013-06-04 16:13:12">, description: "A great new website for the client.", image: "/image.jpg", service: "Web", featured: true, created_at: nil, updated_at: nil> to be valid, but got errors: Client can't be blank

这是我的factory.rb文件:

FactoryGirl.define do
  factory :user do
    sequence(:email)        { |n| "person_#{n}@example.com"}
    password                "secret"
    password_confirmation   "secret"

    factory :admin do
      after(:create) {|user| user.add_role(:admin)}
    end
  end

  factory :client do
    sequence(:name)          { |n| "Client Number #{n}"}
    sequence(:client_code)   { |n| "CN#{n}"}
  end

  factory :work do
    name            "Client Website"
    client
    date            1.day.ago
    description     "A great new website for the client."
    image           "/image.jpg"
    service         "Web"
    featured        true
  end  
end

我的工作模式:

class Work < ActiveRecord::Base
  attr_accessible :client, :client_ids, :date, :description, :featured, :image, :name, :service

  validates_presence_of :client_ids
  validates_presence_of :date, :image, :name
  validates_length_of   :description, :in => 5..500, :allow_blank => true
  validates_length_of   :name, :in => 5..50

  has_many :postings
  has_many :clients, :through => :postings  
end

我的客户模型:

class Client < ActiveRecord::Base
  attr_accessible :client_code, :name

  validates_presence_of     :client_code, :name
  validates_uniqueness_of   :name
  validates_length_of       :client_code, :is => 3
  validates_length_of       :name, :in => 5..50

  has_many  :postings
  has_many  :works, :through => :postings  
end

似乎该测试应该通过,因为工厂正在与客户建立。 但是我不确定为什么验证会导致错误。

错误消息显示expected ..... to be valid, but got errors: Client can't be blank

work = FactoryGirl.build(:work) # this returns an unsaved object

通过使用build您尚未保存该对象。 该对象为nil 如果对象nil ,则为blank

您想像这样保存创建时的对象

work = FactoryGirl.create(:work) # this returns a saved object

现在

work.should be_valid

应该通过

更新:

我对您的`factories.rb'的关注不够

factory :work do
  name            "Client Website"
  client
  date            1.day.ago
  description     "A great new website for the client."
  image           "/image.jpg"
  service         "Web"
  featured        true
end  

您在work工厂中的client属性实际上是空白的。 这不会通过验证,也不会保存对象。 在工厂中填写client ,或在创建时传递类似的内容

work = FactoryGirl.create(:work, client: "fill in with client")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM