gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:36:58 26 4
gpt4 key购买 nike

我正在尝试运行开箱即用测试,但它们在一个独特的列上失败了。还有其他人遇到这个问题吗?

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 文件并不能修复它。 (我见过人们对非唯一数据有疑问)

我正在使用 Rails 5.1.6 和 Ruby 2.4.1p111。

感谢任何帮助。

谢谢

账单

最佳答案

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

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

你能看到发生了什么吗?为您的 create 操作生成的测试 rails 使用 first people fixture 的 name 属性。把它改成这样:

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

这样就可以了。祝你好运。

关于ruby-on-rails - Ruby Rails 5.1.6 测试在唯一列上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50731060/

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