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

标签 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

最佳答案

就我而言,测试 View 会使数据库处于不清楚的状态,您应该尝试 https://github.com/bmabey/database_cleaner它用于 cucumber 测试后的清洁,但主页上提供了 Rspec 的示例。

关于ruby-on-rails - RailsTutorial - 第 8.4.3 章 - 在集成测试中添加用户后未清除测试数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5454373/

相关文章:

javascript - 在另一个模板 rails 内成型

ruby-on-rails - rails 3 : undefined method `remote_form_for'

ruby-on-rails - 如何在使用 vim 时快速访问 ruby​​ 和 rails 文档?

ruby-on-rails - 在rails项目中使用两个或多个数据库

ruby-on-rails - 工厂女孩设计用户工厂无效 - 未通过 RSpec 测试

ruby-on-rails - 在 Rails 中为 http 请求强制使用 SSL

ruby-on-rails - 如何使用 Rails 在 PostgreSQL 中创建日期范围

ruby-on-rails - Rails 单选按钮 - 模型中多列的一种选择

ruby - rspec - 选择之一的匹配器

ruby-on-rails - 创建记录似乎没有持久化到测试数据库,rspec,factory_girl_rails