2011-12-07 23 views
15

Avere molti problemi a ottenere tutte le anatre nel giusto ordine con FactoryGirl.FactoryGirl: Factory not registered: user (ArgumentError)

Impostare un'applicazione Rails minimalista (3.0.11), factory_girl_rails (1.4.0), factory_girl (2.3.2) & cetriolo-rotaie (1.2.1) e Ruby-1.8.7-P352.

Il test cetriolo è:

Feature: a 
    Scenario: test factory-girl 
    Given the following user exists: 
    | name | email    | 
    | Brandon | [email protected] | 

Il risultato è questo:

cucumber 
Using the default profile... 
"Adam Advertiser" 
"[email protected]" 
#<User id: nil, name: nil, created_at: nil, updated_at: nil, email: nil> 
Feature: a 

    Scenario: test factory-girl  # features/users.feature:2 
    Given the following user exists: # factory_girl-2.3.2/lib/factory_girl/step_definitions.rb:100 
     | name | email    | 
     | Brandon | [email protected] | 
     **Factory not registered: user (ArgumentError)** 
     features/users.feature:3:in `Given the following user exists:' 

Failing Scenarios: 
cucumber features/users.feature:2 # Scenario: test factory-girl 

1 scenario (1 failed) 
1 step (1 failed) 
0m0.107s 

Sembrerebbe un problema sequenza di carico, forse. Gradirei qualsiasi aiuto per farlo bene. Saluti Ross

caratteristiche/Factories.rb è quindi:

FactoryGirl.define do 
    factory :user do 
     name 'Adam Advertiser' 
     email '[email protected]' 
    end 
end 
pp FactoryGirl.create(:user) 

require 'factory_girl/step_definitions' 

mio caratteristiche/supporto/env.rb è:

require 'pp' 
require 'cucumber/rails' 

require 'factory_girl_rails' 

mio Gemfile è:

source 'http://rubygems.org' 

gem 'rails', '3.0.11' 

gem 'sqlite3' 


group :development, :test do 

    gem 'cucumber-rails' 
    gem 'factory_girl_rails'  # rails 3 version 

end 

risposta

0

Sono nuovo di factorygirl e ho affrontato un errore simile, come menzionato in questo errore Post.The ho affrontato è stato

ArgumentError: 
    Not registered: client 

impostazione di fabbrica:

/spec/fabbriche .rb

Factory.define :user do |f| 
    f.email { Faker::Internet.email } 
    f.first_name { Faker::Name.first_name } 
    f.last_name { Faker::Name.last_name } 
    f.phone { Faker::PhoneNumber.phone_number } 
    f.password "foobar" 
end 

/spec/factories/users.rb - Con fabbriche bambino tained per: utente

Factory.define :client, :parent => :user do |client| 
    client.email     { Factory.next(:client_email) } 
    client.password     "password" 
    client.password_confirmation "password" 
end 

stavo correndo la specifica utilizzando seguente comando:

app_root$ RAILS_ENV=custom_test bundle exec rspec spec/controllers/users_controller_spec.rb 

Grazie a @ Ross.The file di Gem mostrato me cliccato quello che mancava.

Grazie.

0

ho risolto questo problema è sufficiente aggiungere queste righe sul mio spec_helper:

require 'factory_girl' 
FactoryGirl.find_definitions 
0

Come da this answer, aggiungendo FactoryGirl.find_definitions funzionerà, ma aggiungendo require 'rails_helper' dovrebbe eliminare la necessità di che, se non lo hai fatto.

Problemi correlati