![]() The assertSeeText() function can be used to check if a response has some specific text. I like how “Ok” is a little more expressive and you don’t need to remember 200 is the “correct” response code. ![]() $response->assertStatus(200) can also be replaced by $response->assertOk(). $response = $this -> get ( '/' ) $response -> assertStatus ( 200 ) You can then use assertStatus() to check the return code. The get() function is used to run an HTTP GET request against your application. Our next post in this series will discuss factory classes more so subscribe if you want to know more about them. The factory() facade is used to generate an instance of a class using it’s factory class: $user = factory ( \App\User :: class ) -> create () rand ( 0, 999999 ) $this -> assertDatabaseMissing ( 'projects', ) // do thing to create project $this -> assertDatabaseHas ( 'projects', ) factory() $randomName = 'testCanSubmitWithCorrectData'. I like to start the test above with a call to assertDatabaseMissing() to make sure I’m not testing for something that’s already in the database. $this -> assertDatabaseMissing ( 'database_table_name', ) rand ( 0, 999999 ) // do thing to create project $this -> assertDatabaseHas ( 'projects', ) assertDatabaseMissing()ĪssertDatabaseMissing() in the inverse of assertDatabaseHas() and it’s used to run a database query against a table to see if a set of conditions have not been met. One of the things I like to do is to create a “random” name that I can then query to see if my actions have been successful. $this -> assertDatabaseHas ( 'database_table_name', ) assertDatabaseHas()ĪssertDatabaseHas() is used to run a database query against a table to see if a set of conditions have been met. This section will discuss a couple of them. Laravel has some very helpful functions and facades to make testing easier. I always change the testExample() function to test the index route: public function testIndex () Helper Functions This creates the following class: get ( '/' ) $response -> assertStatus ( 200 ) } } php artisan make:test ProjectControllerTest To create our test class to test our ProjectController we can run the following command. These tests are slower so you won’t want to run them all the time but if you have a continuous integration server it can run them for you.įeature tests are stored in the “tests/Feature/”” directory.įor our examples, we’re going to create a Project module. This will give you a quick way to make sure you didn’t break anything inadvertently. I highly recommend you setup a feature test for each controller before you create the controller so you can test each route as you add it to the controller. One of the things this command does that is super nice is that it distinguishes between tests that are used to test an endpoint (feature tests) and specific functions (unit tests). When Should You Use ItĪll the time when you’re doing Test Driven Development (TDD).Ĭreating a new controller? Run this first.Ĭreating a new model? Run this first. The make:test command creates a unit test class so you can test your application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |