我不知道单元测试是如何工作的。
我有一个返回 JSON 响应的 Controller
Controller.php
public function getDiscount(Request $request)
{
if (isset($request) && !empty($request)) {
return response()->json($this->discount->calculateDiscount($request->json()->all()))->getOriginalContent();
}
}
对于 postman ,这是这条路线的结果:
客户发布:
{
"customer-id": "3",
"items": [
{
"product-id": "A101",
"quantity": "2",
"unit-price": "9.75",
"total": "19.50"
},
{
"product-id": "A102",
"quantity": "1",
"unit-price": "49.50",
"total": "49.50"
}
],
"total": "69.00"
}
API 响应
{
"applied_discounts": [
{
"id": 3,
"name": "Tools Discount",
"description": "Seems like you really like Tools, here's one for free!"
}
],
"discounted_items": [
{
"product-id": "A101",
"quantity": "2",
"unit-price": "9.75",
"total": "19.50",
"discounted_price": 15.6
}
],
"discounted_price": 65.1,
"original_price": "69.00"
}
现在当我尝试进行单元测试时,这就是我想出的:
public function testToolsDiscount()
{
$this->json('POST', '/discount',
[
'customer-id' => '3',
'items' => [
[
'product-id' => 'A101',
'quantity' => '2',
'unit-price' => '9.75',
'total' => '19.50'
],
[
'product-id' => 'A102',
'quantity' => '1',
'unit-price' => '49.50',
'total' => '49.50'
]
],
'total' => '69.00'
])
->seeJson(
[
'applied_discounts' => [
[
'id' => 3,
]
],
]);
}
但是当我运行它时,这是我得到的错误
DiscountTest::testToolsDiscount Invalid JSON was returned from the route. Perhaps an exception was thrown?
我做错了什么?
最佳答案
确保您的路线匹配指定的 '/discount'
及其可能具有的任何前缀。
关于php - 流明/Laravel : Can't figure out how unittesting a json response works,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51293530/