![]() First, you may use the seeInDatabase helper to assert that data exists in the database matching a given set of criteria. Lumen also provides a variety of helpful tools to make it easier to test your database driven applications. $response->assertJsonValidationErrors('name', $responseKey = null) Unlike Laravel, Lumen returns errors at the root of the returned JSON object: public function testApplication() If you are using the assertJsonValidationErrors method, you should pass null as the response key that contains the response's error message. Of course, this data will be available in your routes and controller via the Request instance: $response = $this->call('POST', '/user', ) JSON Validation Errors ![]() If you are making POST, PUT, or PATCH requests you may pass an array of input data with the request. $this->assertEquals(200, $response->status()) If you would like to make a custom HTTP request into your application and get the full Illuminate\Http\Response object, you may use the call method: public function testApplication() The actingAs helper method provides a simple way to authenticate a given user as the current user: create() If you would like to verify that the given array is an exact match for the JSON returned by the application, you should use the seeJsonEquals method: post('/user', ) So, if there are other properties in the JSON response, this test will still pass as long as the given fragment is present. The seeJson method converts the given array into JSON, and then verifies that the JSON fragment occurs anywhere within the entire JSON response returned by the application. To get started, let's write a test to make a POST request to /user and assert that a given array was returned in JSON format: json('POST', '/user', ) You may also easily pass data and headers to these methods. For example, the get, post, put, patch, and delete methods may be used to issue requests with various HTTP verbs. Lumen also provides several helpers for testing JSON APIs and their responses. Lumen provides a very fluent API for making HTTP requests to your application and examining the output. Note: If you define your own setUp method within a test class, be sure to call parent::setUp. ![]() To run your tests, simply execute the phpunit command from your terminal: assertTrue(true) You may then define test methods as you normally would using PHPUnit. To create a test case, simply create a new test file in the tests directory. The testing environment variables may be configured in the phpunit.xml file. You are free to create other testing environment configurations as necessary. Lumen automatically configures the cache to the array driver while testing, meaning no cache data will be persisted while testing. After installing a new Lumen application, simply run phpunit on the command line to run your tests. ![]() The framework also ships with convenient helper methods allowing you to expressively test your application's JSON responses.Īn ExampleTest.php file is provided in the tests directory. In fact, support for testing with PHPUnit is included out of the box, and a phpunit.xml file is already setup for your application. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |