Francisco Morales
Francisco Morales
Laravel
Comparte:

No pasan mis tests | Laravel API

Hola
Me encuentro haciendo el curso de API y no consigo hacer pasar los test. 
Este es el output al ejecutar mis tests.
Espero me puedan dar una pista de como solucionarlo. 
Gracias
PHPUnit\Framework\ExpectationFailedException]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Exception\ExpectationFailedException.php#L24\^]8;;\ {#3007
  #comparisonFailure: null
  #serializableTrace: array:19 [
    0 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Constraint\Constraint.php"
      "line" => 55
      "function" => "fail"
      "class" => "PHPUnit\Framework\Constraint\Constraint"
      "type" => "->"
    ]
    1 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Assert.php"
      "line" => 2344
      "function" => "evaluate"
      "class" => "PHPUnit\Framework\Constraint\Constraint"
      "type" => "->"
    ]
    2 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Assert.php"
      "line" => 1064
      "function" => "assertThat"
      "class" => "PHPUnit\Framework\Assert"
      "type" => "::"
    ]
    3 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\AssertableJsonString.php"
      "line" => 144
      "function" => "assertTrue"
      "class" => "PHPUnit\Framework\Assert"
      "type" => "::"
    ]
    4 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php"
      "line" => 757
      "function" => "assertFragment"
      "class" => "Illuminate\Testing\AssertableJsonString"
      "type" => "->"
    ]
    5 => array:5 [
      "file" => "C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php"
      "line" => 43
      "function" => "assertJsonFragment"
      "class" => "Illuminate\Testing\TestResponse"
      "type" => "->"
    ]
    6 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php"
      "line" => 124
      "function" => "Tests\{closure}"
      "class" => "Illuminate\Testing\TestResponse"
      "type" => "->"
    ]
    7 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php"
      "line" => 1603
      "function" => "macroCall"
      "class" => "Illuminate\Testing\TestResponse"
      "type" => "->"
    ]
    8 => array:5 [
      "file" => "C:\laragon\www\jsonapi\tests\Feature\Articles\CreateArticleTest.php"
      "line" => 70
      "function" => "__call"
      "class" => "Illuminate\Testing\TestResponse"
      "type" => "->"
    ]
    9 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php"
      "line" => 1527
      "function" => "title_is_required"
      "class" => "Tests\Feature\Articles\CreateArticleTest"
      "type" => "->"
    ]
    10 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php"
      "line" => 1133
      "function" => "runTest"
      "class" => "PHPUnit\Framework\TestCase"
      "type" => "->"
    ]
    11 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestResult.php"
      "line" => 722
      "function" => "runBare"
      "class" => "PHPUnit\Framework\TestCase"
      "type" => "->"
    ]
    12 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php"
      "line" => 885
      "function" => "run"
      "class" => "PHPUnit\Framework\TestResult"
      "type" => "->"
    ]
    13 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestSuite.php"
      "line" => 678
      "function" => "run"
      "class" => "PHPUnit\Framework\TestCase"
      "type" => "->"
    ]
    14 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestSuite.php"
      "line" => 678
      "function" => "run"
      "class" => "PHPUnit\Framework\TestSuite"
      "type" => "->"
    ]
    15 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\TestRunner.php"
      "line" => 670
      "function" => "run"
      "class" => "PHPUnit\Framework\TestSuite"
      "type" => "->"
    ]
    16 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\Command.php"
      "line" => 143
      "function" => "run"
      "class" => "PHPUnit\TextUI\TestRunner"
      "type" => "->"
    ]
    17 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\Command.php"
      "line" => 96
      "function" => "run"
      "class" => "PHPUnit\TextUI\Command"
      "type" => "->"
    ]
    18 => array:5 [
      "file" => "C:\laragon\www\jsonapi\vendor\phpunit\phpunit\phpunit"
      "line" => 92
      "function" => "main"
      "class" => "PHPUnit\TextUI\Command"
      "type" => "::"
    ]
  ]
  #message: """
    Unable to find JSON fragment: \r\n
    \r\n
    [{"source":{"pointer":"\/data\/attributes\/title"}}]\r\n
    \r\n
    within\r\n
    \r\n
    [{"data":{"attributes":{"content":"Contenido del art\u00edculo","slug":"nuevo-articulo","title":"Nuevo art\u00edculo"},"id":"1","links":{"self":"http:\/\/jsonapi.dev.com\/api\/v1\/articles\/1"},"type":"articles"}}].\n
    Failed asserting that false is true.
    """
  #code: 0
  #file: "]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Constraint\Constraint.php#L121\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Constraint\Constraint.php]8;;\"
  #line: 121
  trace: {
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Constraint\Constraint.php#L121\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Constraint\Constraint.php:121]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Constraint\Constraint.php#L55\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Constraint\Constraint.php:55]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Assert.php#L2344\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Assert.php:2344]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Assert.php#L1064\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\Assert.php:1064]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\AssertableJsonString.php#L144\C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\AssertableJsonString.php:144]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php#L757\C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:757]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php#L43\C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php:43]8;;\ {
      Illuminate\Testing\TestResponse->Tests\{closure}]8;;file://C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php#L43\^]8;;\
      › $this->assertJsonFragment([
      › ]8;;file://C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php#L43\    'source' => ['pointer' => '/data/attributes/' . $attribute]]8;;\
      › ]);
      arguments: {
        $data: array:1 [ …1]
      }
    }
    ]8;;file://C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php#L124\C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:124]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php#L1603\C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:1603]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\tests\Feature\Articles\CreateArticleTest.php#L70\C:\laragon\www\jsonapi\tests\Feature\Articles\CreateArticleTest.php:70]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php#L1527\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php:1527]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php#L1133\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php:1133]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestResult.php#L722\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestResult.php:722]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php#L885\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestCase.php:885]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestSuite.php#L678\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestSuite.php:678]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestSuite.php#L678\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\Framework\TestSuite.php:678]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\TestRunner.php#L670\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\TestRunner.php:670]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\Command.php#L143\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\Command.php:143]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\Command.php#L96\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\src\TextUI\Command.php:96]8;;\ { …}
    ]8;;file://C:\laragon\www\jsonapi\vendor\phpunit\phpunit\phpunit#L92\C:\laragon\www\jsonapi\vendor\phpunit\phpunit\phpunit:92]8;;\ { …}
  }
}
Process finished with exit code 1
Jorge García
Jorge García (2780 xp)
Hola Francisco, en qué test te aparece esto? puedes poner el contenido del test aquí?
Francisco Morales
Francisco Morales (360 xp)
Hola Jorge

Se trata del CreateArticleTest. acá dejo el codigo:

<?php

namespace Tests\Feature\Articles;

use App\Models\Article;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Testing\TestResponse;
use Tests\TestCase;

class CreateArticleTest extends TestCase
{
    use RefreshDatabase;


    /** @test */
    public function can_create_articles()
    {
        $this->withoutExceptionHandling();
        $response = $this->postJson(route('api.v1.articles.store'), [
           'data' => [
               'type' => 'articles',
               'attributes' => [
                   'title' => 'Nuevo artículo',
                   'slug' => 'nuevo-articulo',
                   'content' => 'Contenido del artículo',
               ]
           ]
        ]);
        $response->assertCreated();
        $article = Article::first();
        $response->assertHeader(
            'Location',
            route('api.v1.articles.show', $article)
        );
        $response->assertExactJson([
           'data' => [
               'type' => 'articles',
               'id' => (string) $article->getRouteKey(),
               'attributes' => [
                   'title' => 'Nuevo artículo',
                   'slug' => 'nuevo-articulo',
                   'content' => 'Contenido del artículo',
               ],
               'links' => [
                   'self' => route('api.v1.articles.show', $article)
               ]
           ]
        ]);
    }

    /** @test */
    public function title_is_required()
    {
        $response = $this->postJson(route('api.v1.articles.store'), [
            'data' => [
                'type' => 'articles',
                'attributes' => [
                    'title' => 'Nuevo artículo',
                    'slug' => 'nuevo-articulo',
                    'content' => 'Contenido del artículo',
                ]
            ]
        ]); //se puede usar ->dump() para ver detalle de error
        /*se espera un error de validacion en el campo title*/


        /*$response->assertJsonStructure([
            'errors' => [
                ['title', 'detail', 'source' => ['pointer']]
            ]
        ])->assertJsonFragment([
            'source' => ['pointer' => '/data/attributes/title']
        ]);*/

        $response->assertJsonApiValidationErrors('title');

    }

    /** @test */
    public function title_must_be_at_least_4_characters()
    {
        $response = $this->postJson(route('api.v1.articles.store'), [
            'data' => [
                'type' => 'articles',
                'attributes' => [
                    'title' => 'Nue',
                    'slug' => 'nuevo-articulo',
                    'content' => 'Contenido del artículo',
                ]
            ]
        ]); //se puede usar ->dump() para ver detalle de error
        /*se espera un error de validacion en el campo title*/
        $response->assertJsonApiValidationErrors('title');

    }

    /** @test */
    public function slug_is_required()
    {
        $response = $this->postJson(route('api.v1.articles.store'), [
            'data' => [
                'type' => 'articles',
                'attributes' => [
                    'title' => 'Nuevo articulo',
                    'content' => 'Contenido del artículo',
                ]
            ]
        ]); //se puede usar ->dump() para ver detalle de error
        /*se espera un error de validacion en el campo title*/
        $response->assertJsonApiValidationErrors('slug');

    }

    /** @test */
    public function content_is_required()
    {
        $response = $this->postJson(route('api.v1.articles.store'), [
            'data' => [
                'type' => 'articles',
                'attributes' => [
                    'title' => 'Nuevo articulo',
                    'slug' => 'nuevo-articulo',
                ]
            ]
        ]); //se puede usar ->dump() para ver detalle de error
        /*se espera un error de validacion en el campo content*/
        $response->assertJsonApiValidationErrors('content');

    }
}


Al ejecutar los tests desde el mismo run test de php Stom, apareceTesting started at 2:30 ...
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.


Failed to find a JSON:API validation error for key: 'title'

Unable to find JSON fragment: 

[{"source":{"pointer":"\/data\/attributes\/ title"}}]

within

[{"data":{"attributes":{"content":"Contenido del art\u00edculo","slug":"nuevo-articulo","title":"Nuevo art\u00edculo"},"id":"1","links":{"self":"http:\/\/jsonapi.test\/api\/v1\/articles\/1"},"type":"articles"}}].
Failed asserting that false is true.
 C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php:53
 C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:124
 C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:1600
 C:\laragon\www\jsonapi\tests\Feature\Articles\CreateArticleTest.php:76
 

Failed to find a JSON:API validation error for key: 'title'

Unable to find JSON fragment: 

[{"source":{"pointer":"\/data\/attributes\/ title"}}]

within

[{"errors":[{"detail":"The data.attributes.title must be at least 4 characters.","source":{"pointer":"\/data\/attributes\/title"},"title":"The given data was invalid."}]}].
Failed asserting that false is true.
 C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php:53
 C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:124
 C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:1600
 C:\laragon\www\jsonapi\tests\Feature\Articles\CreateArticleTest.php:94
 

Failed to find a JSON:API validation error for key: 'slug'

Unable to find JSON fragment: 

[{"source":{"pointer":"\/data\/attributes\/ slug"}}]

within

[{"errors":[{"detail":"The data.attributes.slug field is required.","source":{"pointer":"\/data\/attributes\/slug"},"title":"The given data was invalid."}]}].
Failed asserting that false is true.
 C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php:53
 C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:124
 C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:1600
 C:\laragon\www\jsonapi\tests\Feature\Articles\CreateArticleTest.php:111
 

Failed to find a JSON:API validation error for key: 'content'

Unable to find JSON fragment: 

[{"source":{"pointer":"\/data\/attributes\/ content"}}]

within

[{"errors":[{"detail":"The data.attributes.content field is required.","source":{"pointer":"\/data\/attributes\/content"},"title":"The given data was invalid."}]}].
Failed asserting that false is true.
 C:\laragon\www\jsonapi\tests\MakesJsonApiRequest.php:53
 C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:124
 C:\laragon\www\jsonapi\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:1600
 C:\laragon\www\jsonapi\tests\Feature\Articles\CreateArticleTest.php:128
 


Time: 00:12.814, Memory: 30.00 MB


FAILURES!
Tests: 5, Assertions: 12, Failures: 4.
Process finished with exit code 1




Francisco Morales
Francisco Morales (360 xp)
finalmente.. luego de mirar un poco el repo, solo tenia el test tittle is required,, y yo estaba eviando el title. 

 Failed to find a JSON:API validation error for key: 'title'

Unable to find JSON fragment: 

[{"source":{"pointer":"\/data\/attributes\/title"}}]

within

[{"data":{"attributes":{"content":"Contenido del art\u00edculo","slug":"nuevo-articulo","title":"Nuevo art\u00edculo"},"id":"1","links":{"self":"http:\/\/jsonapi.test\/api\/v1\/articles\/1"},"type":"articles"}}].

Failed asserting that false is true 

con un dum() pude comprobar visualmente lo que me indicaba el error (esperaba falso y era true)

{#1407
  +"data": {#1418
    +"type": "articles"
    +"id": "1"
    +"attributes": {#1422
      +"title": "Nuevo artículo"
      +"slug": "nuevo-articulo"
      +"content": "Contenido del artículo"
    }
    +"links": {#1412
      +"self": "http://jsonapi.test/api/v1/articles/1"
    }
  }
}


Así queda el test finalmente:

/** @test */
public function title_is_required()
{
    $response = $this->postJson(route('api.v1.articles.store'), [
        'data' => [
            'type' => 'articles',
            'attributes' => [
                'title' => '',
                'slug' => 'nuevo-articulo',
                'content' => 'Contenido del artículo',
            ]
        ]
    ]); 

    $response->assertJsonApiValidationErrors('title');

}


ahora continúo.
Saludos