【发布时间】: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