Laravel assertions: The difference between asserts.
Tags: laravel, php, testing
Published: 2020-02-16 22:09:12

##Laravel assertions: The difference between asserts.
I'm sure that most of us have had the doubt of which assertion to use when we are testing our application.
Many times we use them without knowing how they really work.


It's true that some of them are confusing, but each one has it own purpose, even though they are similar.

Let's see some of them.

###assertOk() Vs assertSuccessful()

Let's see what `assertOk()` does:

```php
public function assertOk()
{
    PHPUnit::assertTrue(
        $this->isOk(),
        'Response status code ['.$this->getStatusCode().'] does not match expected 200 status code.'
    );

    return $this;
}


public function isOk(): bool
{
    return 200 === $this->statusCode;
}
```
As we can see, this assertion show us that the response status code must be excatly *200*.

This assertion is useful for cases when we want to check exactly this status *200*. For example, when we make a *get()* request.

Now, let's see what `assertSuccessful()` does.


```php
public function assertSuccessful()
{
    PHPUnit::assertTrue(
        $this->isSuccessful(),
        'Response status code ['.$this->getStatusCode().'] is not a successful status code.'
    );

    return $this;
}


public function isSuccessful(): bool
{
    return $this->statusCode >= 200 && $this->statusCode < 300;
}
```
This assertion is not precisely to verify a *200* status response code.
If we use this to check an HTTP response and the response code is *200*, it will pass.
But, what would happen if the response is 201 (Created) or 204 (No content)?
Do we want this response code or we really want a 200?

[Check HTTP Response Status codes here.](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)


###assertSee() Vs assertSeeText()

Let's see what `assertSee()` does:

```php
public function assertSee($value)
{
    PHPUnit::assertStringContainsString((string) $value, $this->getContent());

    return $this;
}
```
So, what this assertion does, is getting the content of the response and then checks if the value we are passing is within the content.

For example, if we want to check if the content has "My App" in somewhere, we do this:

```php
$this->assertSee('My App');
```

But what if the content is wrapped inside a span tag like this:

```html
My <span class='text-blue'>App</span>
```

The assertion we did before will fail. But why?

Because in the content we have `My <span class='text-blue'>App</span>` and we are looking for `My App` without anything else around.

So, this assertion is helpful when we want to check a whole HTML tag too, for example, if we want
to check that in the content we have a link to Google and the link text is "Google", we could do this.

```php
$this->assertSee('<a href="https://www.google.com/">Google</a>');
```

<br/>
Now, let's see what `assertSeeText()` does.


```php
public function assertSeeText($value)
{
    PHPUnit::assertStringContainsString((string) $value, strip_tags($this->getContent()));

    return $this;
}
```

Can you see the difference?

Yes, the only difference is the `strip_tags()` function.

This function removes all HTML and PHP tags in the content, so in the previous example (`My <span class='text-blue'>App</span>`), the `strip_tags()` will remove the `<span>` tag and the content will be `My App`.

Then, what will happen if we have `My <span class='text-blue'>App</span>` and we do

```php
 $this->assertSeeText('My App');
```

It will work, because all HTML and PHP tags will be removed before it checks if the string is within the content.