Json Assert失败Laravel 5.5 [英] Json Assert Fails Laravel 5.5
问题描述
我正在使用laravel 5.5制作闪存卡应用程序 因此,我正在尝试进行第一个测试,但无法通过.
I am making a flash cards app with laravel 5.5 So I am trying to make the first tests but I can't make it pass.
Failed asserting that an array has the subset Array &0 (
'data' => Array &1 (
'id' => 2020
)
).
--- Expected
+++ Actual
@@ @@
- [data] => Array
- (
- [id] => 2020
- )
-
/Users/marcosantana/devel/cards/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:290
/Users/marcosantana/devel/cards/tests/Unit/CardTest.php:26
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
测试代码:
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use App\User;
use App\Card;
class CardTest extends TestCase
{
use RefreshDatabase;
use DatabaseMigrations;
public function testCardsResponds()
{
//$user = factory(\App\User::class)->create(['password' => "password"]);
//$payload = ['email' => $user->email, 'password' => "password"];
factory(\App\Card::class)->create(['id' => 2020]);//Creates a specific card
$response = $this->json('GET', 'api/cards');
$response->assertJson(['data' => ["id" => 2020]]);
/*
$response
->assertStatus(200)
->assertSuccessful()
->assertJson(['{id}' => 2020]);
// dd($response);
*/
}
}
routes/api.php
The routes/api.php
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('/cards', function(Request $request) {
return \App\Card::all();
});
服务器输出 http://localhost:8000/api/cards
[
{
id: 2020,
tags: "{"lang": "st", "pictures": false}",
front: "<html><head><title>Optio sit consequuntur vel excepturi fuga eum amet.</title></head><body><form action="example.net" method="POST"><label for="username">ut</label><input type="text" id="username"><label for="password">excepturi</label><input type="password" id="password"></form><a href="example.org">Sunt ea quia molestias quod consequatur occaecati earum.</a></body></html>
",
back: "<html><head><title>Et qui vel odit qui repellendus nihil architecto.</title></head><body><form action="example.net" method="POST"><label for="username">ratione</label><input type="text" id="username"><label for="password">iste</label><input type="password" id="password"></form><i>Quia dolor voluptas aut et rem natus ut provident sint enim ullam est.</i>Laboriosam voluptas in aut dignissimos accusamus cupiditate molestias vitae.</body></html>
",
created_at: "2017-12-20 10:07:38",
updated_at: "2017-12-20 10:07:38",
deleted_at: null
}
]
因此,这似乎是一个显而易见的解决方案,但我看不到任何帮助将非常有用.当然这一定很容易,但是我真的被淹没了
So it seems that is an obvious solution but I can not see it any help will be very useful. Surely it must be something easy but I am really swamped
谢谢!!不要觉得抱歉.您非常有帮助. 实际上,我想扩展一下我的问题.现在,我正在尝试验证JSON的其余部分,但"tags"(mysql json)字段给我错误 我尝试这个:
Thank you!! do not be sorry. You were very helpful. Actually I would like to expand a little my question. Now I am trying to validate the rest of the json but the "tags" (mysql json) field gives me error I try this:
$response
->assertStatus(200)
->assertSuccessful()
->assertJson([
[
'id' => 2020,
'tags' => json_encode(array('lang' => 'mh', 'pictures' => 'false'))
]
]);
我明白了:
Failed asserting that an array has the subset Array &0 (
0 => Array &1 (
'id' => 2020
'tags' => '{"lang":"mh","pictures":"false"}'
)
).
--- Expected
+++ Actual
@@ @@
Array
(
[0] => Array
(
[id] => 2020
- [tags] => {"lang":"mh","pictures":"false"}
+ [tags] => {"lang": "mh", "pictures": "false"}
您对此有何看法?再次感谢你! @MarcinNabialek
What are your thoughts on this ? Thank you again! @MarcinNabialek
推荐答案
您的代码有2个问题:
- 您的回复中没有
data
,您将data
传递给assertJson
-
assertJson
方法需要字符串而不是数组
- There is no
data
in your response and you passdata
toassertJson
assertJson
method expects string and not array
以您的情况代替:
$response->assertJson(['data' => ["id" => 2020]]);
您应该使用:
$response->seeJsonSubset([["id" => 2020]]);
这篇关于Json Assert失败Laravel 5.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!