2013-10-27 52 views
1

我是一名测试新手,使用minitest,开始时很困难。我想知道为什么每次运行测试时都会重新创建数据库。我看过示例视频,但似乎并非如此(such as this railscast on minitest)。我的设置不正确?这应该每次都发生吗?为什么每次运行测试时都会创建数据库?

我每次运行rake minitest:models我看到下面的在我的测试运行(缩短,因为它就像500线)

rake minitest:models 
Connecting to database specified by database.yml 
    (0.7ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
    (379.5ms) DROP DATABASE IF EXISTS "mf_test" 
    (239.8ms) CREATE DATABASE "mf_test" ENCODING = 'utf8' 
NOTICE: CREATE TABLE will create implicit sequence "admins_id_seq" for serial column "admins.id" 
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "admins_pkey" for table "admins" 
    (7.8ms) CREATE TABLE "admins" ("id" serial primary key, "email" character varying(255) DEFAULT '' NOT NULL, "encrypted_password" character varying(255) DEFAULT '' NOT NULL, "reset_password_token" character varying(255), "reset_password_sent_at" timestamp, "remember_created_at" timestamp, "sign_in_count" integer DEFAULT 0, "current_sign_in_at" timestamp, "last_sign_in_at" timestamp, "current_sign_in_ip" character varying(255), "last_sign_in_ip" character varying(255), "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL) 
    (1.5ms) CREATE UNIQUE INDEX "index_admins_on_email" ON "admins" ("email") 

... 
... 
... 

Rack::File headers parameter replaces cache_control after Rack 1.5. 
Run options: 

# Running tests: 
... 

test_helper.rb中

ENV["RAILS_ENV"] = "test" 
require File.expand_path("../../config/environment", __FILE__) 
require "rails/test_help" 
require 'minitest/rails' 
require 'minitest/focus' 

# To add Capybara feature tests add `gem "minitest-rails-capybara"` 
# to the test group in the Gemfile and uncomment the following: 
require "minitest/rails/capybara" 

# Uncomment for awesome colorful output 
require "minitest/pride" 

class ActiveSupport::TestCase 
    # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. 
    fixtures :all 

    # Add more helper methods to be used by all tests here... 
    def self.prepare 
    # Add code that needs to be executed before test suite start 
    end 
    prepare 

    def setup 
    # Add code that need to be executed before each test 
    end 

    def teardown 
    # Add code that need to be executed after each test 
    end 
end 

application.rb中

require File.expand_path('../boot', __FILE__) 

require "active_record/railtie" 
require "action_controller/railtie" 
require 'rake/testtask' 
require "action_mailer/railtie" 
require "active_resource/railtie" 
require "sprockets/railtie" 
require "minitest/rails/railtie" 

if defined?(Bundler) 
    Bundler.require(*Rails.groups(:assets => %w(development test))) 
end 

module Martinfurniture 
    class Application < Rails::Application 
    # Configure the default encoding used in templates for Ruby 1.9. 
    config.encoding = "utf-8" 

    # Configure sensitive parameters which will be filtered from the log file. 
    config.filter_parameters += [:password] 

    # Enable escaping HTML in JSON. 
    config.active_support.escape_html_entities_in_json = true 

    # Allows for sub-directories in Models 
    config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')] 

    config.active_record.whitelist_attributes = true 

    config.autoload_paths += %W(#{config.root}/app/models/ckeditor) 

    config.sass.debug_info = true 

    config.exceptions_app = self.routes 

    # Enable the asset pipeline 
    config.assets.enabled = true 

    # Version of your assets, change this if you want to expire all your assets 
    config.assets.version = '1.0' 

    # Devise 
    config.assets.initialize_on_precompile = false 

    # Testing 
    config.generators do |g| 
     g.test_framework :mini_test 
     g.helper false 
     g.assets false 
     g.view_specs false 
    end 
    end 
end 
+1

根据TDD原则'''可重复'''测试数据库应该在每次测试运行时被清除。所以数据库丢失似乎是预期的行为。 – freemanoid

+1

谢谢,有没有办法让它不打印数据库创建(它创造了很多视觉混乱)? –

回答

0

您可以关闭所有SQL查询的打印ActiveRecord::Base.logger = nil

+0

ooops,这应该是为了回应: >谢谢,有没有办法让它不打印数据库创建(它创造了很多视觉混乱)? - 6小时前的欢迎 – fremn

相关问题