gpt4 book ai didi

ruby-on-rails - 为什么这个带有 post 请求的集成测试失败了?

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:52 25 4
gpt4 key购买 nike

我已经为写入两个表/模型的注册表单编写了集成测试。它失败并显示消息 "Organization.count"didn't change by 1。但是,有没有一种方法可以查看测试失败的错误消息类型(该表单在开发中有效,尽管显示错误消息存在问题,所以我不明白测试失败的原因)。所以我的意思是,如果您在服务器上做了同样的事情,您会看到错误消息,以帮助我找出它没有保存组织/用户的原因。

  test "valid combined organization user signup" do
get new_path
assert_template 'organizations/new'
assert_difference ['Organization.count', 'User.count'], 1 do
post organizations_path, organization: { name: "Abc1",
bag: "aef1",
users_attributes: [email: "test@test.br",
username: "abc1",
password: "foobar",
password_confirmation: "foobar"] }
end
end

也许测试日志可以提供帮助?我在这里看到用户电子邮件和用户名的 IS NULL LIMIT。鉴于测试语法,我不明白为什么...

Started POST "/organizations" 
Processing by OrganizationsController#create as HTML
Parameters: {"organization"=>{"name"=>"Abc def 1", "bag"=>"adef1", "users_attributes"=>[{"email"=>"adef1@uniqu.br", "username"=>"adef1", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}]}}
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mOrganization Exists (0.6ms)[0m SELECT 1 AS one FROM "organizations" WHERE LOWER("organizations"."name") = LOWER('Abc def 1') LIMIT 1
[1m[36mOrganization Exists (0.3ms)[0m [1mSELECT 1 AS one FROM "organizations" WHERE LOWER("organizations"."bag") = LOWER('adef1') LIMIT 1[0m
[1m[35mOrganization Exists (0.3ms)[0m SELECT 1 AS one FROM "organizations" WHERE LOWER("organizations"."name") = LOWER('Abc def 1') LIMIT 1
[1m[36mOrganization Exists (0.3ms)[0m [1mSELECT 1 AS one FROM "organizations" WHERE LOWER("organizations"."bag") = LOWER('adef1') LIMIT 1[0m
[1m[35mSQL (0.4ms)[0m INSERT INTO "organizations" ("name", "bag", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "Abc def 1"], ["bag", "adef1"], ["created_at", "2015-06-27 16:28:56.179789"], ["updated_at", "2015-06-27 16:28:56.179789"]]
[1m[36mUser Exists (0.6ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1[0m
[1m[35m (0.6ms)[0m SELECT "users"."email" FROM "users" ORDER BY "users"."username" ASC
[1m[36mUser Exists (0.2ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."username" IS NULL LIMIT 1[0m
[1m[35m (0.2ms)[0m ROLLBACK TO SAVEPOINT active_record_1

Controller 方法发布在这里:stackoverflow.com/q/31072646/4499505 (organizations_path指的是create方法)

如果我将测试日志与开发日志进行比较(因为在开发中表单确实有效),对于开发,它会在下面发布帖子,这似乎与测试日志中的帖子不同。我应该如何调整测试语法以创建与开发中相同的帖子?

Parameters: {"utf8"=>"✓", "authenticity_token"=>"***", "organization"=>{"name"=>"test60", "bag"=>"tes60", "users_attributes"=>{"0"=>{"email"=>"test60@example.com", "username"=>"test60", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}}, "commit"=>"Register"}

更新:为了匹配开发日志,我将测试中的 post 行更改为:

post organizations_path, organization: { name: "Abc def 1",
bag: "adef1",
users_attributes: { "0" => {email: "adef1@uniqu.br",
username: "adef1",
password: "foobar",
password_confirmation: "foobar"}} }
end

现在在测试日志中它不再有 IS NULL LIMIT 但它仍然回滚因此测试仍然失败并显示相同的消息:

  [1m[35mSQL (0.5ms)[0m  INSERT INTO "organizations" ("name", "bag", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "Abc def 1"], ["bag", "adef1"], ["created_at", "2015-06-27 19:49:45.430750"], ["updated_at", "2015-06-27 19:49:45.430750"]]
[1m[36mUser Exists (0.4ms)[0m [1mSELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('adef1@uniqu.br') LIMIT 1[0m
[1m[35m (0.5ms)[0m SELECT "users"."email" FROM "users" ORDER BY "users"."username" ASC
[1m[36mUser Exists (0.5ms)[0m [1mSELECT 1 AS one FROM "users" WHERE LOWER("users"."username") = LOWER('adef1') LIMIT 1[0m
[1m[35m (0.2ms)[0m ROLLBACK TO SAVEPOINT active_record_1

最佳答案

您的测试代码与您的开发代码不一样。您需要在 users_attributes 数组中添加方括号:

test "valid combined organization user signup" do
get new_path
assert_template 'organizations/new'
assert_difference ['Organization.count', 'User.count'], 1 do
post organizations_path,
organization: { name: "Abc1",
bag: "aef1",
users_attributes: { "0" => { email: "test@test.br",
username: "abc1",
password: "foobar",
password_confirmation: "foobar" } } }
end
end

这就是为什么您的日志说它正在尝试查找 email IS NULL 的用户,因为它无法检索 email code> 属性正确,所以它只是默认为 NULL 而不是 test@test.br


编辑:更新以匹配工作语法。 OP 还报告说,一旦语法得到修复,User 模型就会出现错误,没有通过 username 的最小长度验证,他通过简单地使用更长的用户名来修复在测试数据中。

关于ruby-on-rails - 为什么这个带有 post 请求的集成测试失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31090554/

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