【问题标题】:Rails is using fixtures without my permissionRails 在未经我允许的情况下使用固定装置
【发布时间】:2017-09-10 15:51:17
【问题描述】:

我从一个简单的测试开始:

class UserTest < ActiveSupport::TestCase
  test 'should not create user without email' do
    user = User.new
    assert_not user.save
  end
end

Rails 自动生成了一些固定装置:

one:
  email: MyString
  login_token: MyString
  token_generated_at: 2017-09-10 17:24:55

two:
  email: MyString
  login_token: MyString
  token_generated_at: 2017-09-10 17:24:55

如您所见,我没有使用任何固定装置进行测试。但是在执行测试时,Rails 会抛出异常:

Error:
UserTest#test_should_not_create_user_without_email:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("email", "login_token", "token_date", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', '2017-09-10 17:24:55', '2017-09-10 15:48:11.483364', '2017-09-10 15:48:11.483364', 298486374)

起初我认为我可以简单地删除固定装置。但稍后应该有一些固定装置,例如非唯一电子邮件。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 fixtures


    【解决方案1】:

    为了加载固定装置,Rails 不需要您的权限。来自documentation

    测试环境会自动加载所有的夹具到 每次测试前的数据库。为确保数据一致, 环境会在运行加载之前删除固定装置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多