سؤال

I'm a testing newbie using minitest and having a difficult time getting started. I'm wondering why every time I run tests the DB is recreated. I've seen example videos where that doesn't seem to be the case (such as this railscast on minitest). Is my setup incorrect? Should this happen every time?

Every time I run rake minitest:models I see the below before my tests run (shortened, because it's like 500 lines)

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
هل كانت مفيدة؟

المحلول

You can turn off the the printing of all the sql queries with ActiveRecord::Base.logger = nil

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top