2013-06-26 18 views
24

Voglio testare un modello con RSpec ma probabilmente mi sono imbattuto in un errore di battitura che non riesco a trovare. Qualcuno può aiutarmi un po '? Ho lottato con esso per molto tempo e non riesco a trovare errori. Grazie in anticipo!Fabbrica non registrata

user_spec.rb

require 'spec_helper' 

describe User do 
    it "has a valid factory" do 
     FactoryGirl.build(:user).should be_valid 
    end 
    it "is invalid without an e-mail" 
    it "is invalid without a correct e-mail" 
    it "is invalid without a password" 
    it "is invalid without a matching password confrimation" 
end 

user.rb

FactoryGirl.define do 
    factory :user do |f| 
     f.email "[email protected]" 
     f.password "ruby" 
     f.password_confrimation "ruby" 
    end 
end 

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 
require 'factory_girl' 

# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 

RSpec.configure do |config| 
    # ## Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    # Run specs in random order to surface order dependencies. If you find an 
    # order dependency and want to debug it, you can fix the order by providing 
    # the seed, which is printed after each run. 
    #  --seed 1234 
    config.order = "random" 
end 

errore

Factory not registered: user 
+1

Controlla http://stackoverflow.com/questions/4303329/rspec-cant-find-factorys-from-factorygirl. Penso che tu abbia bisogno della gemma factory_girl_rails. –

+0

Ce l'ho già. elenco delle gemme | grep rspec * output è rspec-rails (2.13.2) – Kert

+0

factory_girl_rails è diverso da rspec_rails –

risposta

30

Hai la definizione di fabbrica nel file sbagliato, in base alla tua domanda è in user.rb. Questo deve essere in un factories.rb nella cartella di test (spec) se si utilizza rspec

# user.rb 

FactoryGirl.define do 
    factory :user do |f| 
     f.email "[email protected]" 
     f.password "ruby" 
     f.password_confrimation "ruby" 
    end 
end 

Change sopra a questo, (anche non è necessario la variabile f)

# spec/factories.rb 

FactoryGirl.define do 
    factory :user do 
     email "[email protected]" 
     password "ruby" 
     password_confrimation "ruby" 
    end 
end 

inoltre, come i commenti che dicono, assicurarsi gem 'factory_girl_rails' è in tuo Gemfile, invece di gem 'factory_girl'

+0

In apparenza avevo torto sul nome factories.rb, almeno sulla base del test che ho appena fatto. Se farai una piccola modifica alla tua risposta, rimuoverò il mio downvote. Mi dispiace per quello Avrei potuto giurare ... –

+4

La definizione potrebbe andare in 'spec/factories/user.rb' e funzionare bene. Il file 'spec/factories.rb' potrebbe diventare davvero gonfio nel tempo se non fosse possibile dividere le definizioni di fabbrica. –

+0

@PaulFioravanti Concordato. Per semplicità, dal momento che non c'era indicazione 'utente.rb' era vicino alla cartella 'spec', ho appena suggerito' factories.rb' perché pensavo che fosse più facile da capire. – fontno

0

avevo incontrato il seguente problema con la struttura nelle mie fabbriche cartella/ - factories/ -- artists.rb -- techniques.rb

artists.rb

FactoryBot.define do 
    factory :artist do 
     name  'Michael' 
     technique FactoryBot.create :technique 
    end 
    end 

techniques.rb

FactoryBot.define do 
    factory :technique do 
    name 'Some name' 
    end 
end 

Così stava caricando artista prima tecnica oggetto sono stati caricati. Quindi non poteva trovarlo. La soluzione è di non utilizzare FactoryBot annidato per creare nelle fabbriche o rinominare le fabbriche nidificate con qualcosa che si trova prima della fabbrica principale.

Ho appena spostato la mia fabbrica di tecnica. a factories.rb e lo ha definito lì. E il problema è stato risolto.

Problemi correlati