2012-01-03 22 views
7

tôi đang cố gắng để làm một thử nghiệm đơn giản cho mô hình của tôi Course, tôi đã viết nhà máy này:FactoryGirl, tại sao tôi đã đăng ký hoặc chưa được khởi tạo?

FactoryGirl.define do 
    factory :course do 
    name 'How to be happy ?' 
    end 
end 

các course_spec.rb:

require "rspec" 
require 'factory_girl_rails' 
describe "When a course is created" do 

    it "can't be deleted if any student is enrolled to it" do 
    FactoryGirl.find_definitions 
    course = FactoryGirl.build(:course) 

    student= Student.create! 
    course.students << student 
    course.destroy 
    course.name.should !=nil 
    end 
end 

nhưng, tôi đạt được dòng này

course = FactoryGirl.build(:course) 

Tôi gặp lỗi:

FactoryGirl::DuplicateDefinitionError: Factory already registered: course 

nếu tôi nhận xét này: định nghĩa khóa học trong các nhà máy, tôi nhận được:

NameError: uninitialized constant Course 

Bất kỳ ý tưởng xin vui lòng?

đây là Gem Envioronment tôi:

RubyGems Environment: 
RUBYGEMS VERSION: 1.8.10 
RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [i686-linux] 
INSTALLATION DIRECTORY: /home/sam/.rvm/gems/ruby-1.9.3-p0 
RUBY EXECUTABLE: /home/sam/.rvm/rubies/ruby-1.9.3-p0/bin/ruby 
EXECUTABLE DIRECTORY: /home/sam/.rvm/gems/ruby-1.9.3-p0/bin 
RUBYGEMS PLATFORMS: 
    ruby 
    x86-linux 
GEM PATHS: 
    /home/sam/.rvm/gems/ruby-1.9.3-p0 
    /home/sam/.rvm/gems/[email protected] 
GEM CONFIGURATION: 
    :update_sources => true 
    :verbose => true 
    :benchmark => false 
    :backtrace => false 
    :bulk_threshold => 1000 
REMOTE SOURCES: 
    http://rubygems.org/ 
---------------------- 
IDE: JetBrains RubyMine (EAP) RM-112.291, build #RM-112.291 
OS: Linux 3.0.0-12-generic[i386] 
Java: 1.6.0_23-b23 
RubyMine SDK Environment: 
Sdk: RVM: ruby-1.9.3-p0 
Sdk Version: ver.1.9.3p0 (revision 33570) p0 
Ruby Interpreter: /home/sam/.rvm/rubies/ruby-1.9.3-p0/bin/ruby 
RVM Sdk: yes, gemset:[default] 
RVM Home: /home/sam/.rvm 
Sdk Language Level: 1.9 
Sdk Load Path: 
    /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1 
    /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/i686-linux 
    /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby 
    /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/vendor_ruby/1.9.1 
    /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/vendor_ruby/1.9.1/i686-linux 
    /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/vendor_ruby 
    /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1 
    /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/i686-linux 
    /home/sam/rubyMine4beta/rubystubs19 
Sdk Gem paths: 
    file:///home/sam/.rvm/gems/ruby-1.9.3-p0/bundler/gems 
    file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems 
    file:///home/sam/.rvm/gems/[email protected]/gems 
Gems used for 'hope': 
    ffi (1.0.11, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/ffi-1.0.11) 
    sass (3.1.12, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/sass-3.1.12) 
    coffee-script-source (1.2.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/coffee-script-source-1.2.0) 
    mail (2.3.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/mail-2.3.0) 
    activesupport (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.0) 
    i18n (0.6.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/i18n-0.6.0) 
    uglifier (1.2.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/uglifier-1.2.1) 
    sprockets (2.0.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/sprockets-2.0.3) 
    foreigner (1.1.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/foreigner-1.1.1) 
    treetop (1.4.10, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/treetop-1.4.10) 
    haml (3.1.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/haml-3.1.4) 
    ansi (1.4.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/ansi-1.4.1) 
    mime-types (1.17.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/mime-types-1.17.2) 
    rack (1.3.6, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.3.6) 
    devise (1.5.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/devise-1.5.2) 
    warden (1.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/warden-1.1.0) 
    diff-lcs (1.1.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/diff-lcs-1.1.3) 
    activeresource (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/activeresource-3.1.0) 
    sass-rails (3.1.5, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/sass-rails-3.1.5) 
    therubyracer (0.9.9, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/therubyracer-0.9.9) 
    rspec-expectations (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-expectations-2.7.0) 
    coffee-rails (3.1.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/coffee-rails-3.1.1) 
    activemodel (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.1.0) 
    simple_form (1.5.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/simple_form-1.5.2) 
    bundler (1.0.21, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/bundler-1.0.21) 
    thor (0.14.6, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/thor-0.14.6) 
    activerecord (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.0) 
    json (1.6.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/json-1.6.4) 
    coffee-script (2.2.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/coffee-script-2.2.0) 
    execjs (1.2.13, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/execjs-1.2.13) 
    turn (0.8.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/turn-0.8.3) 
    rake (0.9.2.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2) 
    kaminari (0.13.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/kaminari-0.13.0) 
    rdoc (3.12, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rdoc-3.12) 
    rspec (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-2.7.0) 
    actionpack (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.1.0) 
    rspec-rails (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-rails-2.7.0) 
    guard (0.10.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/guard-0.10.0) 
    cocoon (1.0.15, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/cocoon-1.0.15) 
    libv8 (3.3.10.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/libv8-3.3.10.4-x86-linux) 
    rspec-core (2.7.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.7.1) 
    jquery-rails (1.0.19, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/jquery-rails-1.0.19) 
    rails (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rails-3.1.0) 
    rack-ssl (1.3.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-ssl-1.3.2) 
    arel (2.2.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/arel-2.2.1) 
    erubis (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/erubis-2.7.0) 
    rb-fsevent (0.4.3.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rb-fsevent-0.4.3.1) 
    builder (3.0.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/builder-3.0.0) 
    orm_adapter (0.0.5, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/orm_adapter-0.0.5) 
    rack-cache (1.0.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-cache-1.0.3) 
    multi_json (1.0.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/multi_json-1.0.4) 
    spork (0.9.0.rc9, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/spork-0.9.0.rc9) 
    tilt (1.3.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/tilt-1.3.3) 
    rack-mount (0.8.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-mount-0.8.3) 
    tzinfo (0.3.31, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/tzinfo-0.3.31) 
    validate_url (0.2.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/validate_url-0.2.0) 
    sqlite3 (1.3.5, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/sqlite3-1.3.5) 
    rspec-mocks (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-mocks-2.7.0) 
    polyglot (0.3.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/polyglot-0.3.3) 
    railties (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.0) 
    hike (1.2.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/hike-1.2.1) 
    guard-spork (0.5.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/guard-spork-0.5.0) 
    factory_girl_rails (1.4.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl_rails-1.4.0) 
    factory_girl (2.3.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.3.2) 
    will_paginate (3.0.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/will_paginate-3.0.2) 
    rack-test (0.6.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-test-0.6.1) 
    client_side_validations (3.1.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/client_side_validations-3.1.4) 
    actionmailer (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.1.0) 
    bcrypt-ruby (3.0.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/bcrypt-ruby-3.0.1) 

EDIT

Các spec_helper.rb file:

require 'rubygems' 
require 'spork' 




    Spork.prefork do 
     # Loading more in this block will cause your tests to run faster. However, 
     # if you change any configuration or code from libraries loaded here, you'll 
     # need to restart spork for it take effect. 
     ENV["RAILS_ENV"] ||= 'test' 
     require File.expand_path("../../config/environment", __FILE__) 
     require 'rspec/rails' 
     require 'rspec/autorun' 

     # 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 
     config.mock_with :rspec 

     # 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 
     end 





    end 

    Spork.each_run do 
     # This code will be run each time you run your specs. 
     FactoryGirl.factories.clear 
     # either this if you have multiple files under 'spec/factories', 
     # or just load the single file, such as 'spec/factories.rb' 
     Dir.glob("#{::Rails.root}/spec/factories/*.rb").each do |file| 
     load "#{file}" 
     end 

    end 
+0

Bạn có một khóa học mô hình được xác định chưa? –

+0

Có, Khóa học được xác định trong các mô hình ứng dụng, được di chuyển và có một bảng cho nó – simo

+0

Hãy hiển thị thêm thông tin về lỗi "hằng số không được khởi tạo" này. Thực sự thực sự khó khăn để gỡ lỗi nó mà không có nó. –

Trả lời

18

Cậu cố gắng loại bỏ dòng này?

FactoryGirl.find_definitions 

Tôi đang sử dụng Factory Girl (với factory_girl_rails đá quý) & shoulda với một dự án 3.1 và không cần phải tải một cách rõ ràng định nghĩa.

+0

Tôi đã xóa FactoryGirl.find_definition khỏi hai tệp: trước: /home/sam/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl_rails-1.4.0/lib/factory_girl_rails/railtie.rb và nó đã hoạt động ! giây: /home/sam/Documents/RoR/course_builder/spec/models/course_spec.rb và hoạt động tốt! – simo

+0

5 năm sau đó - điều này đã giúp tôi ra – Mark

4

Tất cả những gì bạn cần làm là require 'rspec_factory_girl' và sau đó là định nghĩa.

Nói chung nếu bạn đặt nó vào spec_helper.rb, nó sẽ yêu cầu tất cả các tệp trong thư mục spec/, có nghĩa là nếu bạn có nhà máy của bạn được xác định ví dụ trong spec/factories.rb, chúng sẽ được tải tự động.

Tuy nhiên, điều đó yêu cầu bạn thêm require 'spec_helper' ở đầu tệp spec của bạn, có vẻ như bạn không làm.

Nếu bạn không muốn sử dụng tệp spec_helper.rb theo cách này, thì chỉ cần yêu cầu định nghĩa theo cách thủ công, chẳng hạn như require 'factories'.

Một mẹo nhỏ nếu bạn đang sử dụng một cái gì đó giống như spork và cần phải tải lại các nhà máy của bạn bằng tay trước mỗi lần chạy

FactoryGirl.factories.clear 
# either this if you have multiple files under 'spec/factories', 
# or just load the single file, such as 'spec/factories.rb' 
Dir.glob("#{::Rails.root}/spec/factories/*.rb").each do |file| 
    load "#{file}" 
end 

chỉnh sửa: ví dụ hoàn chỉnh của Spork

Spork.each_run do 
    require 'factory_girl_rails' 

    # reload all the models 
    Dir["#{Rails.root}/app/models/**/*.rb"].each do |model| 
    load model 
    end 

    # reload all factories 
    FactoryGirl.factories.clear 
    Dir.glob("#{::Rails.root}/spec/factories/*.rb").each do |file| 
    load "#{file}" 
    end 

    # reload routes 
    YourAppName::Application.reload_routes! 
end 
+0

Khi tôi đặt mã của bạn ở trên trong Spork khởi đầu, tôi nhận được lỗi này: Sử dụng RSpec Tính năng tải trước Rails môi trường tải Spork.prefork khối ... Nhà máy đã đăng ký : gôn (FactoryGirl :: DuplicateDefinitionError) Tôi tự hỏi nhà máy đã đăng ký ở đâu? Tôi đã thêm bản chỉnh sửa trong câu hỏi để hiển thị cho bạn spec_helper.rb. – simo

+0

@SamirSabri bạn đang tải nhà máy ở đâu? Nó hoạt động cho tôi khi tôi tải nó trong khối each_run, xem chỉnh sửa cho toàn bộ tệp. –

+2

hoạt động tốt, nhưng tôi cần phải thêm ** FactoryGirl.sequences.clear ** ngoài factories.clear, nếu không tôi đã nhận lỗi DoubleDefinition cho chuỗi tùy chỉnh của tôi (Mail-Adresses v.v.) – stwienert

0

Trong tôi trường hợp, Điều đó xảy ra khi tôi vô tình đặt hai tệp nhà máy có chứa cùng một khóa như sau:

Rails.root/spec/factories/book.rb 
Rails.root/spec/factories/books.rb 

Điều này là hiển nhiên, nhưng tình huống này xảy ra khi tôi phát triển dưới môi trường không có git (!) Và đặt các tệp nguồn bằng rsync (1). Khi tôi đổi tên book.rb thành books.rb từ máy tính cục bộ và rsync thành dev-env, cả book.rb và books.rb đều tồn tại và điều này đã gây ra tình huống ở đây ;-(

1

Tôi cũng gặp phải lỗi này:

FactoryGirl::DuplicateDefinitionError: Factory already registered: factory_name 

Và tôi cố định vấn đề này bằng cách thêm :require => false trong Gemfile của tôi như thế này một:

gem 'factory_girl_rails', :require => false 

Sau đó yêu cầu nó trong test_helper.rb chỉ một lần bởi có dòng này:

require 'factory_girl_rails' 
1

Đây là những gì làm việc cho tôi trong Rails 4.2.6

Trong rails_helper.rb thêm các librarys

require 'factory_girl' 
require 'factory_girl_rails' 

Thêm các đến phần cấu hình

config.include FactoryGirl::Syntax::Methods 
Các vấn đề liên quan