我寫我的應用程序的典型測試,我通過表單創建一個模型,檢查模型數等於1爲什麼這些Rspec示例不會在數據庫事務中發生?
因爲在測試DB已經多條記錄的測試失敗,該計每次運行我的測試時都會增加。看起來每個例子都不是在事務內部發生的(正在回滾),而且我不知道爲什麼。
我在spec_helper.rb文件,這是應該在一個事務中運行的每個例子這一行:
config.use_transactional_fixtures = true
這裏是我的天賦,保持生成模型對象:
require 'spec_helper'
describe "Admin artwork pages" do
subject { page }
let(:gallery) { FactoryGirl.create(:gallery) }
describe "artwork creation" do
context "with valid attributes" do
it "creates new artwork" do
visit admin_gallery_artworks_path(gallery_id: gallery.id)
click_link 'Add new artwork'
fill_in 'artwork_title', with: 'Still Life'
click_button 'Create Artwork'
page.should have_text 'Successfully created'
Artwork.count.should eq 1
end
end
end
end
這裏是來自Rspec的錯誤消息:
Failures:
1) Admin artwork pages artwork creation with valid attributes creates new artwork
Failure/Error: Artwork.count.should eq 1
expected: 1
got: 153
(compared using ==)
編輯:我的spec_helper.rb文件的內容:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# 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
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
# Include route helpers
config.include Rails.application.routes.url_helpers
#
# Take the FactoryGirl out of FactoryGirl.create
config.include FactoryGirl::Syntax::Methods
末
我使用Rails 4.0.0.rc1,紅寶石1.9.3,FactoryGirl和RSpec形軌道2.13.0感謝您的幫助。
您使用的數據庫是? –
Rails附帶的Sqlite3。 – hlh
您是否在使用燈具? –