我正在使用laravel 5.5制作闪存卡应用程序 所以我试图进行第一次测试,但我无法通过。
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);
*/
}
}
路线/ 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
}
]
所以这似乎是一个明显的解决方案,但我看不出任何帮助会非常有用。当然它一定很容易,但我真的被淹没了
谢谢!!不要觉得抱歉。你非常乐于助人。 其实我想扩大一点我的问题。现在我试图验证json的其余部分,但“标签”(mysql json)字段给我错误 我试试这个:
$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
答案 0 :(得分:2)
您的代码有两个问题:
data
,您将data
传递给assertJson
assertJson
方法需要字符串而不是数组在你的情况下,而不是:
$response->assertJson(['data' => ["id" => 2020]]);
你应该使用:
$response->seeJsonSubset([["id" => 2020]]);
答案 1 :(得分:1)
而不是:
$response->seeJsonSubset([["id" => 2020]]);
&#13;
我只需要使用:
$response
/* ->assertStatus(200)
->assertSuccessful()*/
->assertJson([["id" => 2020]]);
&#13;
我们得到:
PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
.... 4 / 4 (100%)
Time: 900 ms, Memory: 22.00MB
OK (4 tests, 6 assertions)
&#13;
谢谢你们,你们带领我朝着正确的方向前进。