【发布时间】:2021-10-11 04:48:04
【问题描述】:
我正在研究“使用 Rails 4 进行敏捷 Web 开发”一书。其中,在与测试相关的某一时刻,它给出了两个错误
Error:
ProductsControllerTest#test_should_update_product:
ArgumentError: unknown keywords: :id, :product
test/controllers/products_controller_test.rb:42:in `block in <class:ProductsControllerTest>'
Error:
ProductsControllerTest#test_should_create_product:
ArgumentError: unknown keyword: :product
test/controllers/products_controller_test.rb:26:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:25:in `block in <class:ProductsControllerTest>'
在同一个地方,作者指出如何修复它们:在'setup do'中添加
@update = {
title: 'Lorem Ipsum',
description: 'Wibbles are fun!',
image_url: 'lorem.jpg',
price: 19.95
}
在“should_create_product”替换
post products_url, params: { product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title } }
与
post :create, product: @update
并在 'should_update_product' 替换
patch product_url(@product), params: { product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title } }
与
patch :update, id: @product, product: @update
但是,在这些修复之后,测试继续给出相同的错误。如何解决?
编辑: 我试图替换为
post :create, params: { product: @update }
和
patch :update, params: { id: @product, product: @update }
现在它给了我另一个错误:
Error:
ProductsControllerTest#test_should_create_product:
URI::InvalidURIError: bad URI(is not URI?): "http://www.example.com:80create"
test/controllers/products_controller_test.rb:26:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:25:in `block in <class:ProductsControllerTest>'
Error:
ProductsControllerTest#test_should_update_product:
URI::InvalidURIError: bad URI(is not URI?): "http://www.example.com:80update"
test/controllers/products_controller_test.rb:42:in `block in <class:ProductsControllerTest>'
【问题讨论】:
-
你在哪个版本的rails上?这些错误来自 R5 及更高版本,而您的书适用于 R4。
patch :update, params: { id: @product, product: @update }应该在 R5 中修复。 -
我的 Rails 版本是 6.1.4.1。我按你说的换了,现在又给我一个错误
-
patch "/update", params: { id: @product, product: @update } -
Error: ProductsControllerTest#test_should_create_product: ActionController::RoutingError: No route matches [POST] "/create" test/controllers/products_controller_test.rb:26:inblock(2 个级别)在' test/controllers/products_controller_test.rb:25:in block in <class:ProductsControllerTest>'Error: ProductsControllerTest#test_should_update_product: ActionController::RoutingError: No route matches [PATCH] "/update" test/controllers/products_controller_test.rb:42:inblock 在'` -
您的网址中的路径似乎不正确example.com:80create。它可能应该是这样的:example.com:80/products/create。还有example.com:80updateexample.com:80/products/update。
标签: ruby-on-rails testing