RailsTutorial - 第 8.4.3 章 - 在集成测试中添加用户后未清除测试数据库 [英] RailsTutorial - chapter 8.4.3 - Test database not clearing after adding user in integration test
问题描述
我被这个难住了.
到目前为止,本教程中的所有内容都进行得很顺利,但是当我将这段代码添加到我的/spec/requests/users_spec.rb 文件中时,事情就开始变糟了:
Everything on the tutorial has gone smoothly so far, but when I add this chunk of code to my /spec/requests/users_spec.rb file, things start to go south:
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 => "ryan@example.com"
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 ),所有的测试都会通过.但是如果我再次运行测试,它们会失败,因为测试数据库没有清除上面代码添加的记录.
If i clear the test database ( rake db:test:prepare ), all of the tests pass. But if i run the tests again, they fail because the test database doesn't clear the record that the code above added.
我在谷歌上搜索了很多,我发现的大部分内容要么指向 config.use_transactional_fixtures 设置,要么指向代码中的嵌套问题.
I've googled quite a bit, and most of what i found pointed either to the config.use_transactional_fixtures setting, or to a nesting issue in the code.
我很确定这两种情况对我来说都不是.这是我的 spec_helper.rb 文件:
I'm pretty sure that neither of these is the case for me. Here is my spec_helper.rb file:
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:
and here is my 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 => "ryan@example.com"
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 的答案,我就可以完成这项工作.这是我更新的 spec/requests/user_spec.rb 文件:
With mpapis answer, i was able to get this working. Here is my updated spec/requests/user_spec.rb file:
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 => "ryan@example.com"
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
推荐答案
就我而言,测试视图使数据库处于不清楚状态,您应该尝试 https://github.com/bmabey/database_cleaner 用于黄瓜测试后的清洁,但主页上提供了 Rspec 的示例.
As far as I'm concerned testing views leaves database in unclear state, you should try https://github.com/bmabey/database_cleaner it is used for cleaning after cucumber tests, but an example for Rspec is available on the main page.
这篇关于RailsTutorial - 第 8.4.3 章 - 在集成测试中添加用户后未清除测试数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!