我有以下代码:
工厂/ web.rb
FactoryGirl.define do
factory :web do
name 'Name of web'
url 'http://google.es'
after(:create) do |web|
10.times do |i|
web.link << FactoryGirl.create(:link, web: web, url: i, _id: i)
end
end
end
end
工厂/ link.rb
factory :link do
anchor_text 'anchor'
title 'title'
code 200
sequence :url do |n|
"http://google.es/#{n}"
end
end
模型/ web.rb
class Web
include Mongoid::Document
(...)
has_many :link
end
模型/ link.rb
class Link
include Mongoid::Document
field :_id, type: String, default: ->{ Digest::MD5.hexdigest(url) }
belongs_to :web
field :url, type: String
(...)
end
所以我在Link中有一个自定义ID,它在开发环境中工作但是当我运行rspec时,我正在进行所有测试:
Failure/Error: Unable to find matching line from backtrace
TypeError:
can't convert nil into String
# ./app/models/link.rb:4:in `digest'
# ./app/models/link.rb:4:in `hexdigest'
# ./app/models/link.rb:4:in `block in <class:Link>'
# ./spec/factories/webs.rb:10:in `block (4 levels) in <top (required)>'
# ./spec/factories/webs.rb:9:in `times'
# ./spec/factories/webs.rb:9:in `block (3 levels) in <top (required)>'
# ./spec/factories/user.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:43:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
答案 0 :(得分:0)
你可以尝试写:
factory :link do
sequence(:url) { |n| "http://google.es/#{n}" }
anchor_text 'anchor'
title 'title'
code 200
url
end