2014-08-31 2 views
0

나는 다음과 같은 코드가 있습니다Mongoid 및 factoryGirl

공장/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 

models/link.rb

class Link 
    include Mongoid::Document 
    field :_id, type: String, default: ->{ Digest::MD5.hexdigest(url) } 
    belongs_to :web 
    field :url, type: String 
    (...) 
end 

그래서 내가 링크에서 사용자 지정 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

당신이 시도 할 수 쓰기 :

factory :link do 
    sequence(:url) { |n| "http://google.es/#{n}" } 

    anchor_text 'anchor' 
    title 'title' 
    code 200 
    url 
end 
+0

작동하지 않아 오류가 발생합니다 : 특성이 등록되지 않았습니다. – ie8888