Jan 29, 2021
0
Level 11
Testing With API Resource as assertion
Hey there!
Looking for some help here if anyone has it.
Let's say I have an API Resource such as this:
class Resource extends JsonResource
{
public function toArray($request)
{
return [
'foo' => $this->foo,
'bar' => $this->bar
];
}
}
And a test like this:
class ProductTest extends TestCase
{
/**
* @test
*/
public function test_api()
{
$response = $this->get($this->some_url);
$response->assertJsonStructure([
'foo',
'bar'
]);
}
}
Is it possible to do something like this to test for the param returned:
class ProductTest extends TestCase
{
/**
* @test
*/
public function test_api()
{
$response = $this->get($this->some_url);
$response->assertResponse($response, Resource::class);
}
}
I'm constantly writing out everything expected in the JSON response of my tests and would love a way to just test it against the expected response of the API resource.
Thanks in advance!
Please or to participate in this conversation.