2011-03-28 42 views
6

我很困惑這一點。在本教程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 
+0

你說你添加的代碼到你的'spec_helper'文件?它應該在特定的規範中,而不是'spec_helper'。這是一個錯字嗎? – Andrew 2011-03-28 05:13:59

+0

@安德魯是錯字,我編輯它。感謝您注意到這一點。 – 2011-03-28 05:21:18

回答

2

至於我擔心的測試意見葉數據庫不清的狀態,你應該嘗試https://github.com/bmabey/database_cleaner它是用於後黃瓜測試清洗,而是主頁上提供了Rspec的示例。

+0

感謝您的幫助,當我回家時,我會給你一個鏡頭,讓你知道它是如何工作的。 – 2011-03-28 20:57:28

-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