---
agentTools:
  projectIndex: https://laravel-angular.readme.io/llms.txt
---

# API test helpers

This repository makes it easier for you to test REST APIs.
We've added a couple of integration testing helpers that you're to use.

[block:parameters]
{
  "data": {
    "h-0": "test helper",
    "h-1": "Usage",
    "0-0": "seeApiSuccess()",
    "0-1": "Asserts response returned using `success` response macro:\n`return response()->success(..)`",
    "1-0": "seeValidationError()",
    "1-1": "Asserts response code is 422 and that the error is returned (you get that when Laravel throws a validation error or when you `return response()->error('message', 422);`",
    "2-0": "seeApiError($status_code)",
    "2-1": "Asserts for specific status code error.. for example 401.",
    "3-0": "seeJsonKey($key)",
    "3-1": "equivalent to `->see('\"'.$key.'\"'):`",
    "4-0": "seeJsonValue($value)",
    "4-1": "equivalent to `->see('\"'.$value.'\"')`",
    "5-0": "seeJsonArray($entity)",
    "5-1": "equivalent to `->see('\"'.$entity.'\":[')`\nWhich is useful when you expect an array of **Orders** for example.",
    "6-0": "seeJsonObject($entity)",
    "6-1": "equivalent to `->see('\"'.$entity.'\":{')`\nWhich is useful when you expect a single **Order** for example."
  },
  "cols": 2,
  "rows": 7
}
[/block]

[block:callout]
{
  "type": "info",
  "body": "Avoid using bcrypt() in your model factory. As it'll slow down your tests. Only use it if you want to know the password of the user model created (e.g.: in login, reset password tests)."
}
[/block]

Here's an example controller and integration test for it

[block:code]
{
  "codes": [
    {
      "code": "<?php\n\n$api->group([], function ($api) {\n\n    $api->post('posts', 'PostsController@create');\n\n});",
      "language": "php",
      "name": "Routes.php"
    }
  ]
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "<?php\n\nuse App\\Post;\n\nclass PostsController{\n \n   public function create(Request $request)\n   {\n       $this->validate($request, [\n         'name'   => 'required',\n       ]);\n     \n       $post = Post::create([\n         'name' => $request->input('name'),\n         ]);\n     \n       return response()->success(compact('post'));\n   }\n  \n}",
      "language": "php",
      "name": "PostsController.php"
    }
  ]
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "<?php\n\nclass CreatePostTest extends TestCase\n{ \n    public function testCreatingNewPostSuccessfully()\n    {\n        $post = factory(App\\Post::class)->make();//we're not saving it in the database\n      \n        $this->post('/api/posts', ['name' => $post->name])\n        ->seeApiSuccess()\n        ->seeJsonObject('post')\n        ->seeJson(['name' => $post->name]);\n        \n        $this->seeInDatabase('posts', [\n           'name' => $post->name,\n        ]);\n    }\n}\n",
      "language": "php",
      "name": "CreatePostTest.php"
    }
  ]
}
[/block]

[block:callout]
{
  "type": "info",
  "body": "Don't forget the `/api/` at the beginning of the route in your test file."
}
[/block]