问题
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);
*/
}
}
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();
});
The server output 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
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'))
]
]);
And I get this:
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"}
What are your thoughts on this ? Thank you again! @MarcinNabialek
回答1:
There are 2 problems with your code:
- There is no
data
in your response and you passdata
toassertJson
assertJson
method expects string and not array
In your case instead of:
$response->assertJson(['data' => ["id" => 2020]]);
you should rather use:
$response->seeJsonSubset([["id" => 2020]]);
回答2:
Instead of:
$response->seeJsonSubset([["id" => 2020]]);
I just needed to use:
$response
/* ->assertStatus(200)
->assertSuccessful()*/
->assertJson([["id" => 2020]]);
And we get:
PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
.... 4 / 4 (100%)
Time: 900 ms, Memory: 22.00MB
OK (4 tests, 6 assertions)
Thank you guys you lead me in the right direction.
来源:https://stackoverflow.com/questions/47910741/json-assert-fails-laravel-5-5