RailsTutorial - 第8.4.3章 - 在集成测试中添加用户后,测试数据库未清除

时间:2011-03-28 03:31:43

标签: ruby-on-rails ruby rspec

我很难过这个。

到目前为止,教程中的所有内容都进展顺利,但是当我将这段代码添加到我的/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 => "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),则所有测试都通过。但是,如果我再次运行测试,它们会失败,因为测试数据库不会清除上面添加的代码的记录。

我已经搜索了很多,我发现的大部分内容都指向了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 => "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文件:

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

2 个答案:

答案 0 :(得分:2)

就我而言,测试视图使数据库处于不清晰状态时,您应该尝试https://github.com/bmabey/database_cleaner它在黄瓜测试后用于清理,但主页上提供了Rspec的示例。

答案 1 :(得分:-1)

mpapis的答案让它发挥作用。

请务必将其包含在您的GEMFILE中,例如:

group :test do
    gem 'rspec', '2.5.0'
    gem 'webrat', '0.7.1'
    gem 'spork', '0.9.0.rc4'
    gem 'factory_girl_rails'
    gem 'database_cleaner'
end    

bundle install