Laravel UnitTest Course - Essential Assertion Methods

In this post, I'll give you an example of the most important assertion methods to consider when implementing unit or integration tests in Laravel.

assertStatus and assertOk

Video thumbnail

These are the first assertion methods that we will see, the essential ones that we must use first when evaluating the tests, here it would be the one to do assertStatus() or assertOk(), which would be equivalent to assertStatus(200); 200 corresponds to the http code returned in a normal request like the one we have here if we load this page it returns 200 by default since it is the code used by default to indicate that everything is Ok:

$response = $this->get(route('blog.show', ['post'=> $post]))
  ->assertOk() 
  // ->assertStatus(200)

assertSee

Video thumbnail

Another essential assertion method would be assertSee, which tells us what it is that is being seen in the view, we simply pass it a text and that text has to be in the view, in this case this would be a test for the detail one, then it would be this beauty that we have here, the title of the post, the category and the content, then we obtain the post that we generate with its dependencies and here with the post we search for it and look for it to have the title, the content and obviously here the category and well that is what assertSee works for:

$response = $this->get(route('category.index'))
->assertOk()
->assertViewIs('dashboard.category.index')
->assertSee('Category')

AssertViewHas

Video thumbnail

Another essential assertion method that we have to use when we are working with controllers that return a track would be the assertion method, in which we can indicate the name of the parameter and also here we indicate what that parameter has to have, in this case the parameter called post in plural has to have a pagination of only two levels, so, that is what you can see here, which is what we are returning, so that is what the assertion method called AssertViewHas works for:

$this->get(route('permission.create'))
    ->assertOk()
    ->assertSee('Dashboard')
    ->assertSee('Name')
    ->assertSee('Send')
    ->assertViewHas('permission', new Permission());

Test Driven Development (TDD)

There's a programming technique called TDD, which stands for Test-Driven Development. Here's an excerpt from my book where I talk about this technique. In a nutshell, TDD tells us that we should implement tests first—that is, this little file you see here (whether integration tests or unit tests)—before developing our application's functionality.

For example, let's say we're developing this blog feature, which displays a list and details of a post. Applying TDD here makes a lot of sense, since with tests:

We not only verify and ensure that the application works correctly (at least for the cases we've tested).

But we also establish clear rules that our application must follow.

Video thumbnail

I agree to receive announcements of interest about this Blog.

In this post, I'll give you an example of the most important assertion methods to consider when implementing unit or integration tests in Laravel.

- Andrés Cruz

En español