我很困惑这一点。在本教程RailsTutorial - 章节8.4.3 - 在集成测试中添加用户之后,测试数据库未被清除
一切都进展顺利,到目前为止,但是当我的代码这个块添加到我的/spec/requests/users_spec.rb文件,事情开始南下:
describe "success" do
it "should make a new user" do
lambda do
visit signup_path
fill_in "Name", :with => "Example User"
fill_in "Email", :with => "[email protected]"
fill_in "Password", :with => "foobar"
fill_in "Confirmation", :with => "foobar"
click_button
response.should have_selector("div.flash.success",
:content => "Welcome")
response.should render_template('users/show')
end.should change(User, :count).by(1)
end
end
如果我清除测试数据库(rake db:test:prepare),所有测试都通过。但是如果我再次运行测试,它们会失败,因为测试数据库不会清除上面添加的代码的记录。
我已经使用了相当多的东西,并且大部分发现都指向了config.use_transactional_fixtures设置,或者代码中的嵌套问题。
我很确定这些都不适合我。这里是我的spec_helper.rb文件:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
# Needed for Spork
ActiveSupport::Dependencies.clear
end
end
Spork.each_run do
load "#{Rails.root}/config/routes.rb"
Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
end
这里是我users_spec.rb:
describe "Users" do
describe "signup" do
describe "failure" do
it "should not make a new user" do
lambda do
visit signup_path
fill_in "Name", :with => ""
fill_in "Email", :with => ""
fill_in "Password", :with => ""
fill_in "Confirmation", :with => ""
click_button
response.should render_template('users/new')
response.should have_selector("div#error_explanation")
end.should_not change(User, :count)
end
end
describe "success" do
it "should make a new user" do
lambda do
visit signup_path
fill_in "Name", :with => "Example User"
fill_in "Email", :with => "[email protected]"
fill_in "Password", :with => "foobar"
fill_in "Confirmation", :with => "foobar"
click_button
response.should have_selector("div.flash.success",
:content => "Welcome")
response.should render_template('users/show')
end.should change(User, :count).by(1)
end
end
end
end
任何想法?谢谢。
随着mpapis的答案,我能够得到这个工作。这里是我的最新规范/请求/ user_spec.rb文件:
require 'spec_helper'
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
describe "Users" do
describe "signup" do
describe "failure" do
it "should not make a new user" do
lambda do
visit signup_path
fill_in "Name", :with => ""
fill_in "Email", :with => ""
fill_in "Password", :with => ""
fill_in "Confirmation", :with => ""
click_button
response.should render_template('users/new')
response.should have_selector("div#error_explanation")
end.should_not change(User, :count)
end
end
describe "success" do
it "should make a new user" do
lambda do
visit signup_path
fill_in "Name", :with => "Example User"
fill_in "Email", :with => "[email protected]"
fill_in "Password", :with => "foobar"
fill_in "Confirmation", :with => "foobar"
click_button
response.should have_selector("div.flash.success",
:content => "Welcome")
response.should render_template('users/show')
end.should change(User, :count).by(1)
DatabaseCleaner.clean
end
end
end
end
你说你添加的代码到你的'spec_helper'文件?它应该在特定的规范中,而不是'spec_helper'。这是一个错字吗? – Andrew 2011-03-28 05:13:59
@安德鲁是错字,我编辑它。感谢您注意到这一点。 – 2011-03-28 05:21:18