我正在尝试开箱即用,但他们在一个独特的列上失败了。其他人有这个问题吗?
rails new MyApp
rails g scaffold Person name:string:uniq note:text
rails db:migrate
编辑我的夹具:
one:
name: ONE
note: MyText
two:
name: TWO
note: MyText
people_controller_test.rb用于加载初始夹具:
class PeopleControllerTest < ActionDispatch::IntegrationTest
setup do
@person = people(:one)
end
但看起来它正在加载,因为我在数据库中找到它:
sqlite> select * from people;
298486374|TWO|MyText|2018-06-06 23:21:29.686780|2018-06-06 23:21:29.686780
980190962|ONE|MyText|2018-06-06 23:21:29.686780|2018-06-06 23:21:29.686780
我收到了这个错误:
Error:
PeopleControllerTest#test_should_create_person:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: people.name: INSERT INTO "people" ("name", "note", "created_at", "updated_at") VALUES (?, ?, ?, ?)
app/controllers/people_controller.rb:30:in `block in create'
app/controllers/people_controller.rb:29:in `create'
test/controllers/people_controller_test.rb:20:in `block (2 levels) in <class:PeopleControllerTest>'
test/controllers/people_controller_test.rb:19:in `block in <class:PeopleControllerTest>'
bin/rails test test/controllers/people_controller_test.rb:18
.
Finished in 0.848814s, 8.2468 runs/s, 8.2468 assertions/s.
7 runs, 7 assertions, 0 failures, 1 errors, 0 skips
删除test.sqlite3文件无法修复它。 (我见过人们对非uniq数据有问题)
我正在使用Rails 5.1.6和Ruby 2.4.1p111。
感谢任何帮助。
由于
纸币
答案 0 :(得分:1)
我猜测您正在使用包含以下代码的自动生成测试:
test 'create' do
assert_difference 'People.count' do
post people_url, params: { people: { name: people(:one).name }}
end
end
你能看到发生了什么吗?为您的创建操作生成的测试轨使用第一个人夹具的name属性。把它改成这样的东西:
test 'create' do
assert_difference 'People.count' do
post people_url, params: { people: { name: 'Matz' }}
end
end
那可能会这样做。祝你好运。