ruby-on-rails - 工厂女孩创建对象但在数据库中找不到对象

标签 ruby-on-rails factory-bot

我在工厂女孩中使用 cucumber ,这是一个神秘的问题,

在我的 cucumber 步骤中:

fact = Factory.create(:fact,
                   :factable_type => "type_#{i}",
                   :factable_id => i.to_s,
                   :factable_action => "action_#{i}",
                   :priority => "1"
    )

当我使用 ruby​​ mine 检查 Debug模式时,我有一个带有 ID 的事实,但是如果我检查我的 IRB 测试控制台:
ruby-1.9.2-p180 :011 > Fact.all
 => [] 

当然,当我想运行我的测试并检查数据库中对象的存在时,我的测试因此失败。你有什么主意吗 ?

这是我的 env.rb 文件:
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
# It is recommended to regenerate this file in the future when you upgrade to a 
# newer version of cucumber-rails. Consider adding your own code to a new file 
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.

require 'cucumber/rails'
require 'fakeweb'
require "factory_girl"

# Add this to load Capybara integration:
require 'capybara/rspec'
require 'capybara/rails'

require File.expand_path('../../../spec/vcr_setup.rb', __FILE__)
require 'cucumber/rails'
require 'cucumber/rspec/doubles'

require 'email_spec'
require 'email_spec/cucumber'

# require 'cucumber/thinking_sphinx/external_world'
# Cucumber::ThinkingSphinx::ExternalWorld.new

# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
# steps to use the XPath syntax.
Capybara.default_selector = :css

Capybara.run_server = true #Whether start server when testing
Capybara.default_selector = :xpath #default selector , you can change to :css
Capybara.default_wait_time = 2 #When we testing AJAX, we can set a default wait time
Capybara.ignore_hidden_elements = false #Ignore hidden elements when testing, make helpful when you hide or show elements using javascript
Capybara.javascript_driver = :culerity #default driver when you using @javascript tag

# By default, any exception happening in your Rails application will bubble up
# to Cucumber so that your scenario will fail. This is a different from how 
# your application behaves in the production environment, where an error page will 
# be rendered instead.
#
# Sometimes we want to override this default behaviour and allow Rails to rescue
# exceptions and display an error page (just like when the app is running in production).
# Typical scenarios where you want to do this is when you test your error pages.
# There are two ways to allow Rails to rescue exceptions:
#
# 1) Tag your scenario (or feature) with @allow-rescue
#
# 2) Set the value below to true. Beware that doing this globally is not
# recommended as it will mask a lot of errors for you!
#
ActionController::Base.allow_rescue = false

# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
#begin
#  DatabaseCleaner.strategy = :transaction
#rescue NameError
#  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
#end

# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
# See the DatabaseCleaner documentation for details. Example:
#
#   Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
#     DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
#   end
#
#   Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
#     DatabaseCleaner.strategy = :transaction
#   end
#

## Sets up the Rails environment for Cucumber
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails/world'
Cucumber::Rails::World.use_transactional_fixtures
#
##Seed the DB

Fixtures.reset_cache

fixtures_to_load = ['facts','roles', 'user_types']

fixtures_folder = File.join(Rails.root, 'features', 'fixtures')
fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') }
fixtures.delete_if { |fx| !fixtures_to_load.index(fx)}
Fixtures.create_fixtures(fixtures_folder, fixtures)

Before do
    Capybara.default_host = 'http://site.local'

  NetworkConfig.initialize(:id => 1)
end

在我的 factory.rb 文件中:
require "factory_girl"

Factory.define :fact do |i|
  i.factable_type "Follow"
  i.factable_id   "1"
  i.factable_action     "create"
  i.is_processed  "false"
  i.processing_has_started "false"
  i.processing_has_ended "false"
  i.priority "1"
end

最佳答案

如果您正在使用 rails 控制台 rails c 命令来检查您工厂的数据。我认为你需要像这样切换环境:

$ rails c
irb(main):010>ActiveRecord::Base.establish_connection "test"
irb(main):010> Fact.all

关于ruby-on-rails - 工厂女孩创建对象但在数据库中找不到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10248533/

相关文章:

ruby-on-rails - 如何通过 cPanel 在共享主机上部署 Ruby on Rails 应用程序?

ruby-on-rails-3 - 如何定义与工厂女孩/机器人的多态关联

ruby-on-rails - 使用 FactoryGirl 包含在验证和 Rspec 中

ruby-on-rails - 工厂(:some_factory) or FactoryGirl. 创建(:some_factory)

ruby - 如何在 Fabrication 中对属性进行分组,就像在 Factory_Girl 中使用特征一样

ruby-on-rails - Rails 3 - 嵌套资源 - 路由

javascript - 如何在 Rails 4 中利用浏览器缓存?

ruby-on-rails - Model.where(...).first 属性不是 nil 但显示 nil

ruby-on-rails - rails 3 : uninitialized constant FactoryGirl

javascript - Rails 相当于 setInterval?