You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes we need to write a unit test for collections, There are some methods for testing JSON structure in feature test, but in the unit test there is no way to test structures! of course, some assertions exist for checking keys but that is not what I want. I need a method like the assertJsonStructure to do.
Example
assume that we have a resource collection like this:
ExampleResource
<?phpnamespaceApp\Http\Resources;
useIlluminate\Http\Resources\Json\JsonResource;
class ExampleResource extends JsonResource
{
/** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable */publicfunctiontoArray($request)
{
return [
'first_field' => $this->first_field,
'second_field' => SecondExampleResource::collection($this->second_field),
];
}
}
SecondExampleResource
<?phpnamespaceApp\Http\Resources;
useIlluminate\Http\Resources\Json\JsonResource;
class SecondExampleResource extends JsonResource
{
/** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable */publicfunctiontoArray($request)
{
return [
'foo' => $this->foo,
'bar' => $this->bar,
];
}
}
now I want to test when I make a collection from ExampleResource that the keys will be something like this:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Sometimes we need to write a unit test for
collections
, There are some methods for testing JSON structure in feature test, but in the unit test there is no way to test structures! of course, someassertions
exist for checking keys but that is not what I want. I need a method like the assertJsonStructure to do.Example
assume that we have a resource collection like this:
ExampleResource
SecondExampleResource
now I want to test when I make a collection from
ExampleResource
that the keys will be something like this:It would be highly appreciated if anyone can advise me!
Beta Was this translation helpful? Give feedback.
All reactions