我正在尝试使用MiniTest测试我的rails 4应用程序。我有一些种子数据,以获得应用设置。种子数据加载到测试数据库后,我尝试运行测试。
test_seed.rake
namespace :db do
namespace :test do
desc "Load seed data before running tests"
task :prepare => :environment do
puts "\nLoading Seed data.....\n\n"
Rake::Task["db:seed"].invoke
puts "\n....Seed data loaded!\n\n"
end
end
end
完成后,我做了
rake db:test:prepare
然后,当我简单地说'耙''命令(截至目前我没有编写任何测试用例)
test_helper.rb中
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
# 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"
# Code coverage, refer https://github.com/colszowka/simplecov
require 'simplecov'
SimpleCov.start
# Create customizable MiniTest output formats
require "minitest/reporters"
Minitest::Reporters.use!(
Minitest::Reporters::SpecReporter.new,
ENV,
Minitest.backtrace_filter
)
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
include FactoryGirl::Syntax::Methods
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
extend MiniTest::Spec::DSL
end
class ActionDispatch::IntegrationTest
extend MiniTest::Spec::DSL
include Capybara::DSL
end
的Gemfile:
group :test do
# MiniTest
gem 'minitest-spec-rails', '5.2.0'
gem 'minitest-rails-capybara', '2.1.1'
gem 'minitest-reporters', '1.0.5'
gem 'guard-minitest', '2.3.1'
gem 'launchy'
gem 'factory_girl_rails', "~> 4.2.1"
gem 'faker', '1.4.3'
gem 'simplecov', '0.9.2', require: false
end
现有测试,role_test.rb:
require 'test_helper'
class RoleTest < ActiveSupport::TestCase
test "nothing" do
assert true
# role_names = Role.where(name: 'superAdmin').collect(&:name)
# assert_includes role_names, 'SuperAdmin', 'it should include role superAdmin'
end
失败并出现以下错误:
RoleTest
test_0001_nothing ERROR (0.02s)
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "name" violates not-null constraint
DETAIL: Failing row contains (980190962, null, 2015-03-31 19:51:59, 2015-03-31 19:51:59, null, null, null, null, null, null, null).
: INSERT INTO "companies" ("created_at", "updated_at", "id") VALUES ('2015-03-31 19:51:59', '2015-03-31 19:51:59', 980190962)
Fabulous run in 0.05302s
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
任何人都可以帮助我,为什么我得到这个错误?如果可能的话,建议修复它。
由于