在创建工厂对象时传递 id



我想知道如何在为 rspec 测试创建工厂时传递 id。 目前,我可以创建"投资组合"属性,例如在没有关联时,但我不确定何时有关联。示例是我当前的设置

class Portfolio < ActiveRecord::Base
has_many :portfolio_sectors
has_many :sectors, through: :portfolio_sectors

attr_accessible :overview, :title, :url, :sector_ids, :image_id, :images_attributes
#Validations
validates :title, :presence => {:message => 'Add your Title'}
validates :url, :presence => {:message => 'Add a URL'}
validates :overview, :presence => {:message => 'Add an Overview'}
#NOT SURE ON THIS BELOW
validates :sector_ids, :presence => {:message => 'Choose At Least 1 Sector'}

class PortfolioSector < ActiveRecord::Base
belongs_to :portfolio
belongs_to :sector
end

到目前为止,为项目组合对象创建工厂包括以下内容

FactoryGirl.define do
 factory :portfolio do
   id 1
   overview "MyText"
   title "MyText"
   url "http://www.bbc.co.uk"
   sector_id 2
 end
end

和我的规格

require 'spec_helper'
describe Portfolio do
it "has a valid factory" do
 expect(FactoryGirl.build(:portfolio)).to be_valid
end
it "is successful with all attributes" do
  portfolio = FactoryGirl.build(:portfolio)
  expect(portfolio).to be_valid
end
it "is invalid with no Title" do 
 portfolio = FactoryGirl.build(:portfolio, title: nil)
 expect(portfolio).to have(1).errors_on(:title)
end
it "is invalid with no url" do 
 portfolio = FactoryGirl.build(:portfolio, url: nil)
 expect(portfolio).to have(1).errors_on(:url)
end
it "is invalid with no Overview" do 
 portfolio = FactoryGirl.build(:portfolio, overview: nil)
 expect(portfolio).to have(1).errors_on(:overview)
end
it "is invalid with no Sector_id" do 
 portfolio = FactoryGirl.build(:portfolio, sector_ids: '')
 expect(portfolio).to have(1).errors_on(:sector_ids)
end
end

不过,我在运行测试时收到此错误

2) Portfolio has a valid factory
 Failure/Error: expect(FactoryGirl.build(:portfolio)).to be_valid
   expected #<Portfolio id: 1, overview: "MyText", title: "MyText", created_at: nil, updated_at: nil, sector_id: 2, url: "http://www.bbc.co.uk", slug: "mytext"> to be valid, but got errors: Sector ids Choose At Least 1 Sector
 # ./spec/models/portfolio_spec.rb:6:in `block (2 levels) in <top (required)>'

处理此关联类型并对其进行测试的最佳方法是什么?

谢谢

更新

所以我刚刚尝试了这个,但感觉它有点冗长和笨拙

首先,我将工厂对象更新为

 FactoryGirl.define do
 factory :portfolio do
   id 1
   overview "MyText"
   title "MyText"
   url "http://www.bbc.co.uk"
   sector_ids 2  #changed this to sector_ids
 end
end

然后在我的测试数据库中创建了一些扇区,其中一个扇区确实分配了 ID 2。我确定不是最好的方法吗?

问题似乎出在sector_ids . 您是否尝试不直接断sector_ids言,而是间接地断言,例如: sectors { create_list( :sector, 1 ) } ,或者像下面所说的那样。请注意,shell 已经定义了:sector工厂。

其他强烈建议:不要直接对系统进行 rails 字段,例如: id created_at updated_at。所以代码外壳是:

FactoryGirl.define do
   factory :sector do
      # some setups ....
   end
   factory :portfolio do
      overview "MyText"
      title "MyText"
      url "http://www.bbc.co.uk"
      ignore do
         sectors_count 0
      end
      after( :create ) do| portfolio, evaluator |
         create_list( :sector, evaluator.sectors_count, portfolio: portfolio )
      end
   end
end

和用法:

create( :portfolio ).sectors.length # 0
create( :portfolio, sectors_count: 15 ).sectors.length # 15

最新更新