我和Minitest一直在玩,并且发现了我似乎无法找到解释的行为
我有一个非常简单的模型测试文件,如下所示:
require 'minitest_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobards", password_confirmation: "foobards")
end
describe "with admin attribute set to 'true'" do
before { @user.toggle!(:admin) }
it { @user.admin.must_equal true }
end
end
当我在'rake db:test:prepare'之后第一次运行此代码时,测试通过
当我连续第二次运行它时,它会给我一个错误:
test_0001_anonymous 0:00:00.132
错误 SQLite3 :: ConstraintException:列电子邮件不是唯一的:INSERT INTO“users”(“admin”,“created_at”,“email”,“name”, “password_digest”,“updated_at”)VALUES(?,?,?,?,?,?)
如果我拿出
,似乎不会出现这个错误before { @user.toggle!(:admin) }
我的minitest_helper.rb如下:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"
require "capybara/rails"
require "active_support/testing/setup_and_teardown"
class IntegrationTest < MiniTest::Spec
include Rails.application.routes.url_helpers
include Capybara::DSL
register_spec_type(/integration$/, self)
end
class HelperTest < MiniTest::Spec
include ActiveSupport::Testing::SetupAndTeardown
include ActionView::TestCase::Behavior
register_spec_type(/Helper$/, self)
end
Turn.config.format = :outline
我似乎无法理解这是一个错误还是(更有可能)我错过了什么。 有可能比我更了解的人请解释一下吗?