2011-03-14 24 views
10

Đây là cấu hình Gemfile tôi:Không thể có được factory_girl chạy dưới đường ray 3.0.5, bất ngờ tCONSTANT

group :development, :test do 
    gem 'rspec-rails' 
    gem 'factory_girl', '~>2.0.0.beta1' 
    gem 'factory_girl_rails', :git => 'https://github.com/thoughtbot/factory_girl_rails.git', :tag => 'v1.1.beta1' 
end 

Đây là tôi 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 "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} 
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f} 

tôi đã thêm vào thư mục factories đến LOAD_PATH, bởi vì Tôi muốn giữ chúng trong một thư mục riêng biệt.

Đây là factories.rb tập tin của tôi:

đòi hỏi File.expand_path (File.dirname (FILE) + '../../spec_helper')

Factory.define(:user) do |f| 
    f.country("China") 
    ... other attributes here 
end 

Khi tôi chạy thử nghiệm , sử dụng rake spec:models, tôi có được điều này:

spec/factories/factories.rb:1: syntax error, unexpected tCONSTANT, expecting $end 

tôi thấy rằng đây có nguồn gốc từ factory_girl 's 01.234.349 Phương thức. Tôi đã cố tự gọi nó, từ spec_helper, nhưng nó không thay đổi gì cả. Đây là một phần của dấu vết ngăn xếp:

** Invoke spec:models (first_time) 
** Invoke db:test:prepare (first_time) 
** Invoke db:abort_if_pending_migrations (first_time) 
** Invoke environment (first_time) 
** Execute environment 
rake aborted! 

    C:/rails/rcproj/spec/factories/factories.rb:1: syntax error, unexpected tCONSTANT, expecting 
    $end 
      f.count...er) do |f| 
           ^
    C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `req 
    uire' 
    C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `req 
    uire' 
    C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `loa 
    d_dependency' 
    C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new 
    _constants_in' 
    C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `loa 
    d_dependency' 
    C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `req 
    uire' 
    C:/Ruby187/lib/ruby/gems/1.8/gems/factory_girl-2.0.0.beta2/lib/factory_girl/find_definitions.rb:20:i 
    n `find_definitions' 
    C:/Ruby187/lib/ruby/gems/1.8/gems/factory_girl-2.0.0.beta2/lib/factory_girl/find_definitions.rb:19:i 
    n `each' 
    C:/Ruby187/lib/ruby/gems/1.8/gems/factory_girl-2.0.0.beta2/lib/factory_girl/find_definitions.rb:19:i 
+0

Nhận xét dòng đầu tiên (yêu cầu 'spec_helper') và dùng thử. – Ashish

+0

Có một số tiến bộ, thử nghiệm bắt đầu chạy, nhưng tại dòng nói rằng: 'u = Nhà máy (: người dùng) .tạo' nó ném một ArgumentError,' Chưa đăng ký: user', ngoại lệ. – Geo

+0

u = Dòng nhà máy (: người dùng) chỉ tạo người dùng cho bạn – Ashish

Trả lời

26

Tôi nghĩ rằng vấn đề liên quan đến việc tải nhà máy của bạn. Chỉ cần viết này trong tập tin test_helper.rb bạn

require 'factory_girl' 
    Dir.glob(File.dirname(__FILE__) + "/factories/*").each do |factory| 
    require factory 
    end 

    OR 

    require 'factory_girl' 
    FactoryGirl.find_definitions 
+0

Vâng, nó đang tải liên quan. Di chuyển tệp nhà máy trong thư mục spec giải quyết vấn đề. – Geo

+5

btw, thay đổi Factory.find_definitions thành FactoryGirl.find_definitions Factory đã bị ngưng hoạt động – daniel

+0

Chỉ cần làm điều đó và tôi nhận được một thông báo để thay đổi Factory.find_definitions thành FactoryGirl.find_definitions. –

6

tôi đã cùng một vấn đề, nhưng nó quay ra rằng tôi chỉ đơn giản là tên tập tin của tôi sai (factories.rb.rb, nhờ Netbeans). Tuy nhiên, tôi đã phát hiện ra một vài điều hữu ích cho trang này.

  1. Từ getting started page. Nhà máy cô gái sẽ tự động tải các tập tin sau đây nếu chúng tồn tại:

    test/factories.rb 
    spec/factories.rb 
    test/factories/*.rb 
    spec/factories/*.rb 
    

    Đưa sau trong test_helper.rb có thể gây bốc kép:

    require 'factory_girl' Factory.find_definitions

    này gây cho tôi để gặp lỗi `add_as': Already defined: <factory name>.

  2. Đá quý factory_girl_rails tự động tải factory_girl. Nó không cần thiết, nhưng dường như không có bất kỳ tác dụng phụ nào.

  3. Tôi phát hiện ra cú pháp FactoryGirl đã thay đổi khá nhiều kể từ lần đầu tiên tôi học nó vào tháng 3 năm 2011. Tôi cao khuyên mọi người nên xem trang bắt đầu để xem một số thay đổi.

Các vấn đề liên quan