Hi,
I am implementing an API using this package, so far it works great. I am writing tests and the documentation is lacking any information on the subject : https://laravel-json-api.readthedocs.io/en/latest/basics/testing/.
Is there any plan/timeline on when the docs will be updated ? I am currently looking at the project tests to see how it works but it feels a bit lackluster, especially given the testing process seems to have been modified in v3 and few tests have been updated.
Yeah you make a really good point, I really should get round to writing the tests chapter because testing is one of the key strengths of this package.
I'll see if I have some time later this week to do it - but yeah, this should be a priority.
As a quick summary for the moment, you create a test JSON API request using chaining via the jsonApi() method:
$response = $this->jsonApi()
->expects('posts')
->withData($data)
->post('/api/v1/posts');
All the methods have docblocks and can be seen here:
https://github.com/cloudcreativity/laravel-json-api/blob/develop/src/Testing/TestBuilder.php
Then the response has a load of JSON API document assertion methods on it. The main ones to use are:
assertCreatedWithServerId
assertCreatedWithClientId
assertFetchedOne / assertFetchedOneExact
assertFetchedMany / assertFetchedManyExact
assertFetchedNull
assertErrorStatus
Again, in the meantime you can take a look at the docblocks for the available assertion methods here:
https://github.com/cloudcreativity/json-api-testing/blob/develop/src/Concerns/HasHttpAssertions.php
Ok great, thanks for the quick heads-up.
I'll have a look at the docblocks while I wait on the official documentation.
Great! I'm going to re-open this issue as a reminder that I need to add testing docs.
Hello, thank you for your help. I have been having trouble with testing my json api.
You mentioned that I can test using the fluent interface.
$response = $this->jsonApi() ->expects('posts') ->withData($data) ->post('/api/v1/posts');
But where can I use this snippet? I'm on Laravel 8.16 and Laravel JSON API 3.2.
I have tried in tests/Feature/ExampleTest as a minimal example...
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\TestCase;
use Tests\CreatesApplication;
class ExampleTest extends TestCase
{
use CreatesApplication;
public function testJsonApiExample()
{
$response = $this->jsonApi()
->expects('posts')
->post('/api/v1/posts');
}
}
However, when I run php artisan test --filter=ExampleTest I get the following output:
FAIL Tests\Feature\ExampleTest
⨯ json api example
---
• Tests\Feature\ExampleTest > json api example
Error
Call to undefined method Tests\Feature\ExampleTest::jsonApi()
at tests/Feature/ExampleTest.php:15
11â–• use CreatesApplication;
12â–•
13â–• public function testJsonApiExample()
14â–• {
➜ 15▕ $response = $this->jsonApi()
16â–• ->expects('posts')
17â–• ->post('/api/v1/posts');
18â–• }
19â–•
Tests: 1 failed
Time: 0.50s
This makes sense because I am not implementing some kind of trait that will provide $this->jsonApi(). So how is it intended to be accessed?
Hi @DanielSchetritt,
At the moment you only use the CreatesApplication trait in your extension of the TestCase class.
Both trait and class don't implement the function jsonApi().
To use this function, you should add the MakesJsonApiRequests trait with the namespace CloudCreativity\LaravelJsonApi\Testing to your class.
Hi @DanielSchetritt,
At the moment you only use the
CreatesApplicationtrait in your extension of theTestCaseclass.
Both trait and class don't implement the functionjsonApi().
To use this function, you should add theMakesJsonApiRequeststrait with the namespaceCloudCreativity\LaravelJsonApi\Testingto your class.
Perfect this is exactly what I am looking for. Thank you.
There's now full testing documentation here:
https://laraveljsonapi.io/docs/1.0/testing/
Technically this is for the newly redesigned package, but very little changed with the testing. So you should find that testing documentation works for this package too.
Most helpful comment
Great! I'm going to re-open this issue as a reminder that I need to add testing docs.