1

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

每次我运行时,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

应用程序.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
4

1 回答 1

0

您可以使用以下命令关闭所有 sql 查询的打印ActiveRecord::Base.logger = nil

于 2013-10-28T04:21:31.720 回答