作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 Gemfile 配置:
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
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}
factories
文件夹到 LOAD_PATH,因为我想将它们保存在一个单独的文件夹中。
factories.rb
文件:
Factory.define(:user) do |f|
f.country("China")
... other attributes here
end
rake spec:models
,我明白了:
spec/factories/factories.rb:1: syntax error, unexpected tCONSTANT, expecting $end
factory_girl
的
find_definitions
方法。我尝试从 spec_helper 自己调用它,但它没有改变任何东西。这是堆栈跟踪的一部分:
** 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
最佳答案
我认为问题与您工厂的装载有关。
只需将其写入您的 test_helper.rb 文件中
require 'factory_girl'
Dir.glob(File.dirname(__FILE__) + "/factories/*").each do |factory|
require factory
end
OR
require 'factory_girl'
FactoryGirl.find_definitions
关于ruby-on-rails - 无法让 factory_girl 在 rails 3.0.5 下运行,意外的 tCONSTANT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298392/
我目前正在学习“艰难地学习 Ruby”的第 9 课。 我已经完全按照指示输入了第 6 行,但在执行时仍然出现错误。 它说: Syntax error, unexpected tCONSTANT, ex
这是我的 Gemfile 配置: group :development, :test do gem 'rspec-rails' gem 'factory_girl', '~>2.0.0
我从类似的帖子中看到,这通常是前一行中缺少某些结束字符的情况,但我在这里没有看到。 Courses 资源嵌套在 Schools 中,我可以打印出 user_id 和 school.id。 错误似乎出现
我是一名优秀的程序员,十分优秀!