我一直在玩 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:列电子邮件不是唯一的:插入“用户”(“管理员”、“创建的_at”、“电子邮件”、“名称”、“密码摘要”、“更新的_at”)值( ?, ?, ?, ?, ?, ?)
然而,如果我取出这个错误似乎不会发生
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
我似乎无法理解这是一个错误还是(更有可能)我遗漏了一些东西。比我更懂的人能解释一下吗?