Ruby on rails Ruby Rails 5.1.6在唯一列上测试失败

Ruby on rails Ruby Rails 5.1.6在唯一列上测试失败,ruby-on-rails,ruby,testing,Ruby On Rails,Ruby,Testing,我试图运行开箱即用的测试,但它们在一个唯一的列上失败。还有其他人有这个问题吗 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 People

我试图运行开箱即用的测试,但它们在一个唯一的列上失败。还有其他人有这个问题吗

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
我得到了这个错误:

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
错误:
PeopleControllerTest#test(测试)应(创建)人员:
ActiveRecord::RecordNotUnique:SQLite3::ConstraintException:唯一约束失败:人员。名称:插入“人员”(“姓名”、“备注”、“创建时间”、“更新时间”)值(?、、、、?)
app/controllers/people\u controller.rb:30:in'block in create'
app/controllers/people\u controller.rb:29:in'create'
测试/控制器/人员控制器测试。rb:20:in‘block(2级)in’
测试/控制器/人员控制器测试。rb:19:in'block in'
bin/rails测试/controllers/people\u controller\u测试。rb:18
.
以0.848814s、8.2468次运行/秒、8.2468次断言/秒的速度完成。
7次运行,7次断言,0次失败,1次错误,0次跳过
删除test.sqlite3文件并不能修复它。(我看到人们对非uniq数据存在问题)

我正在使用Rails 5.1.6和Ruby 2.4.1p111

感谢您的帮助

谢谢


bill

我猜您正在使用包含以下代码的自动生成测试:

test 'create' do
  assert_difference 'People.count' do
    post people_url, params: { people: { name: people(:one).name }}
  end
end
你能看到发生了什么事吗?为您的创建操作生成的测试rails使用第一个人装置的name属性。将其更改为以下内容:

test 'create' do
  assert_difference 'People.count' do
    post people_url, params: { people: { name: 'Matz' }}
  end
end

那也许行。祝你好运。

你说得对,名字是uniq字段。这是它失败的地方。。。唯一约束失败:人。名称:对,谢谢。您可能无意中使用了一个名称,而没有在测试之间清理数据库。我使用RSpec,所以我不能说更多。
test 'create' do
  assert_difference 'People.count' do
    post people_url, params: { people: { name: 'Matz' }}
  end
end