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 => "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
6
задан Ryan Quinn 29 March 2011 в 03:14
поделиться