【问题标题】:Pass an id when creating a factory object创建工厂对象时传递一个 id
【发布时间】:2014-01-19 15:20:06
【问题描述】:

我想知道在为我的 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)>'

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

谢谢

更新

所以我只是尝试了这个,但觉得它有点啰嗦和hacky

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

 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。我确定这不是最好的方法吗?

【问题讨论】:

  • 向你展示你测试build(:portfolio)).to be_validbuild未保存对象和be_valid数据库写入测试
  • 看来问题出在sector_ids...您是否尝试过不直接使用sector_ids,而是间接使用,例如:sectors { create_list( :sector, 1 ) }。请注意,您的外壳已经拥有:sector 工厂。
  • 另外强烈建议:不要直接评估系统导轨字段,例如:idcreated_atupdated_at...
  • 您还需要答案吗?
  • 一个答案会有所帮助,更像是一个例子,不知道你说的 cmets 是什么意思,谢谢

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


【解决方案1】:

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

其他强烈建议:不要直接评估系统轨道字段,例如:idcreated_atupdated_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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多