2012-02-26 7 views
6

मैं फैक्टरी लड़की में "अनुक्रम" को प्रत्येक परीक्षण के बीच रीसेट करने की कोशिश कर रहा हूं।rspec और कारखाने की लड़की के साथ डुप्लिकेट फैक्ट्री त्रुटि?

(factory_girl 2.6.0 और factory_girl_rails 1.7.0)

मुझे लगता है कि ऐसा करने के लिए, मैं FactoryGirl परिभाषाओं को फिर से लोड करने के लिए है। मैं spec_helper.rb के अंतिम लाइनों में यह कार्य करें:

require 'rubygems' 
require 'spork' 
#uncomment the following line to use spork with the debugger 
#require 'spork/ext/ruby-debug' 

Spork.prefork do 
    ENV["RAILS_ENV"] ||= 'test' 
    require File.expand_path("../../config/environment", __FILE__) 
    require 'rspec/rails' 
    require 'rspec/autorun' 
    require 'capybara/rspec' 

    require "rails/application" 
    Spork.trap_method(Rails::Application::RoutesReloader, :reload!) 

    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

    RSpec.configure do |config| 
    config.mock_with :rspec 

    require 'database_cleaner' 
    config.before(:suite) do 
     DatabaseCleaner.strategy = :transaction 
     DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:each) do 
     DatabaseCleaner.start 
    end 

    config.after(:each) do 
     DatabaseCleaner.clean 
    end 

    config.infer_base_class_for_anonymous_controllers = false 

    # For mailer 
    config.include(MailerMacros) 
    config.before(:each) {reset_email} 
    end 

end 

Spork.each_run do 
    # This code will be run each time you run your specs. 
    I18n.backend.reload! 
    Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 
    require 'factory_girl' 
    FactoryGirl.definition_file_paths = [File.join(Rails.root, 'spec', 'factories')] 
    FactoryGirl.find_definitions 
end 

जोड़ना:

FactoryGirl.definition_file_paths = [File.join(Rails.root, 'spec', 'factories')] 
    FactoryGirl.find_definitions 

निम्न त्रुटि के लिए मुझे बिक्रीसूत्र जब rspec spec/

→ bundle exec guard 
Guard uses Growl to send notifications. 
Guard is now watching at '/Rails/projects/MyRailsProject' 
Starting Spork for RSpec 
Using RSpec 
Preloading Rails environment 
Loading Spork.prefork block... 
Spork is ready and listening on 8989! 
Spork server for RSpec successfully started 
Guard::RSpec is running, with RSpec 2! 
Running all specs 
Exception encountered: #<FactoryGirl::DuplicateDefinitionError: Factory already registered: user> 

जो एक दोहराया गया प्रतीत हो रहा है चल रहा है कारखाना त्रुटि, शायद यह कारखाने की लड़की को दो बार लोड करने की कोशिश कर रही है, लेकिन मुझे समझ में नहीं आता क्यों।

उत्तर

10

यह आपके सभी कारखानों को दो बार लोड करने की कोशिश कर रहा है, क्योंकि आप इसे पूछ रहे हैं।

FactoryGirl.reload 

मौजूदा कारखानों, दृश्यों आदि को साफ जो और फिर साथ find_definitions करने के लिए अपने कॉल की जगह के लिए आप

फोन find_definitions