创建工厂对象时传递id

时间:2014-01-19 15:20:06

标签: ruby ruby-on-rails-3 testing factory-bot

我想知道在为我的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 ...不是最好的方式吗?

1 个答案:

答案 0 :(得分:1)

似乎问题出在sector_ids。您是否尝试直接分配不是sector_ids,而是间接地分配,例如:sectors { create_list( :sector, 1 ) },或者如下所示。请注意,shell已经定义了 :sector 工厂。

其他强有力的建议:不要直接分配系统rails字段,例如: id created_at {{ 1}} 的。所以代码shell应该是:

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