2011-03-28 22 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