gpt4 book ai didi

ruby-on-rails - Minitest 在连续两次运行中给出不同的结果

转载 作者:行者123 更新时间:2023-11-28 20:32:49 25 4
gpt4 key购买 nike

我一直在尝试使用 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
ERROR SQLite3::ConstraintException: column email is not unique: 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

我似乎无法理解这是一个错误还是(更有可能)我遗漏了什么。请比我更了解的人解释一下吗?

最佳答案

toggle!方法保存记录。因此,当您第二次运行测试时,数据库中已经存在一条电子邮件为“user@example.com”的记录。并且保证电子邮件地址唯一性的验证失败。

尝试使用toggle (没有爆炸声)。

关于ruby-on-rails - Minitest 在连续两次运行中给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10864845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com