use*_*241 44 ruby-on-rails ruby-on-rails-3.2 factory-bot
问题描述: - 我设置了factory_girl_rails但是每当我尝试加载工厂时,它都会尝试多次加载它.
Environment:
- rails (3.2.1)
- factory_girl (2.5.2)
- factory_girl_rails (1.6.0)
- ruby-1.9.3-p0 [ x86_64 ]
> rake spec --trace
** Execute environment
-- Creating User Factory
-- Creating User Factory
rake aborted!
Factory already registered: user
Run Code Online (Sandbox Code Playgroud)
我改变的唯一的另一件事是:/config/initializers/generator.rb
Rails.application.config.generators do |g|
g.test_framework = :rspec
g.fixture_replacement :factory_girl
end
Run Code Online (Sandbox Code Playgroud)
的Gemfile
gem 'rails', '3.2.1'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'devise'
gem 'haml-rails'
group :development do
gem 'hpricot'
gem 'ruby_parser'
gem "rspec-rails"
end
group :test do
gem "rspec"
gem 'factory_girl_rails'
end
gem 'refinerycms-core', :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-dashboard', :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-images', :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-pages', :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-resources', :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-settings', :git => 'git://github.com/resolve/refinerycms.git'
group :development, :test do
gem 'refinerycms-testing', :git => 'git://github.com/resolve/refinerycms.git'
end
gem 'refinerycms-inventories', :path => 'vendor/engines'
FactoryGirl.define do
factory :role do
title "MyString"
end
end
Run Code Online (Sandbox Code Playgroud)
这似乎是一个兼容性/环境问题,我似乎无法弄清楚.有什么建议?
编辑:这是我的spec/spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
#require 'factory_girl_rails'
# 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
end
Run Code Online (Sandbox Code Playgroud)
nmo*_*ott 37
factory_girl_rails
应该在gem文件spec_helper.rb
而不是gem文件中需要gem - 你可能需要两次FactoryGirl,这就是你获得副本的原因.
在你的gem文件中试试这个:
group :test do
gem "rspec"
gem 'factory_girl_rails', :require => false
end
Run Code Online (Sandbox Code Playgroud)
然后确保spec_helper中需要工厂女孩:
require 'factory_girl_rails'
Run Code Online (Sandbox Code Playgroud)
顺便说一下 - 你不需要两个rspec
和rpsec-rails
你的gemfile.您可以使用以下内容替换它们:
group :development, :test do
gem 'rspec-rails'
end
Run Code Online (Sandbox Code Playgroud)
您需要在两个组中使用rspec,以便rake任务在开发中起作用,核心测试将在测试中起作用.
Edu*_*eal 16
我也有这个问题.在我的例子中,有两个文件具有相同的代码,如下所示:
FactoryGirl.define do
factory :user do
end
end
Run Code Online (Sandbox Code Playgroud)
一个文件名为"Useres.rb",另一个文件名为"User.rb",因此我只删除了"Useres.rb"并修复了错误.
Rya*_*ger 16
您是否有机会从配置文档中粘贴整个代码段以获取支持文件?
# RSpec
# spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
FactoryGirl.find_definitions
end
end
Run Code Online (Sandbox Code Playgroud)
如果你阅读了评论,你会看到你只想要一个或另一个.我犯了这个错误并得到了问题中陈述的错误.
Mor*_*rty 10
打电话FactoryGirl.define(:user)
或FactoryGirl.find_definitions
两次你也有这个问题.
尝试删除第二个电话或:
FactoryGirl.factories.clear
FactoryGirl.find_definitions
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26822 次 |
最近记录: |