Lumen / Laravel:无法弄清楚json响应的单元测试是如何工作的(Lumen / Laravel: Can't figure out how unittesting a json response works)

我无法弄清楚单元测试的工作原理。

我有控制器返回JSON响应

Controller.php这样

public function getDiscount(Request $request) { if (isset($request) && !empty($request)) { return response()->json($this->discount->calculateDiscount($request->json()->all()))->getOriginalContent(); } }

对于邮递员,这是这条路线的结果:

邮寄客户:

{ "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从路由返回无效的JSON。 也许抛出异常?

我究竟做错了什么?

I can't figure out how unit testing works.

I have controller which returns a JSON response

Controller.php

public function getDiscount(Request $request) { if (isset($request) && !empty($request)) { return response()->json($this->discount->calculateDiscount($request->json()->all()))->getOriginalContent(); } }

With postman, this is the result of this route:

Post by client:

{ "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" }

Response by 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" }

Now when I try to make unit test, this is what I came up with:

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, ] ], ]); }

However when I run it, this is the error I get

DiscountTest::testToolsDiscount Invalid JSON was returned from the route. Perhaps an exception was thrown?

What am I doing wrong?

最满意答案

确保您的路线与指定的'/discount'匹配,并带有任何前缀。

Make sure your route matches the specified '/discount' with any prefix it may have.

Lumen / Laravel:无法弄清楚json响应的单元测试是如何工作的(Lumen / Laravel: Can't figure out how unittesting a json response works)

我无法弄清楚单元测试的工作原理。

我有控制器返回JSON响应

Controller.php这样

public function getDiscount(Request $request) { if (isset($request) && !empty($request)) { return response()->json($this->discount->calculateDiscount($request->json()->all()))->getOriginalContent(); } }

对于邮递员,这是这条路线的结果:

邮寄客户:

{ "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从路由返回无效的JSON。 也许抛出异常?

我究竟做错了什么?

I can't figure out how unit testing works.

I have controller which returns a JSON response

Controller.php

public function getDiscount(Request $request) { if (isset($request) && !empty($request)) { return response()->json($this->discount->calculateDiscount($request->json()->all()))->getOriginalContent(); } }

With postman, this is the result of this route:

Post by client:

{ "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" }

Response by 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" }

Now when I try to make unit test, this is what I came up with:

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, ] ], ]); }

However when I run it, this is the error I get

DiscountTest::testToolsDiscount Invalid JSON was returned from the route. Perhaps an exception was thrown?

What am I doing wrong?

最满意答案

确保您的路线与指定的'/discount'匹配,并带有任何前缀。

Make sure your route matches the specified '/discount' with any prefix it may have.