We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Today, I was adding tests for some Blade templates in Laravel.
One of the things I wanted to assert is that the output contained a specific piece of HTML.
I first tried:
use Tests\TestCase;
class ExternalFlowControllerTest extends TestCase
{
/** @test */
public function it_contains_a_specific_html_snippet(): void
{
$this->get('/my/endpoint')
->assertSee("<input type=\"hidden\" name=\"id\" value=\"expected\">");
}
}
That however didn't work as expected, returning the following error instead:
Expected: <!DOCTYPE html>\n
<html lang="en">\n
<head>\n
... (62 more lines)
To contain: <input type="hidden" name="id" value="expected">
The fix is really easy, there is a second parameter you can add to the
assertSee
function to indicate that the output should not be
escaped. It defaults to true
, so you need to set it to false
to avoid the escaping.
Assert that the given string is contained within the response. This assertion will automatically escape the given string unless you pass a second argument of
false
.
use Tests\TestCase;
class ExternalFlowControllerTest extends TestCase
{
/** @test */
public function it_contains_a_specific_html_snippet(): void
{
$this->get('/my/endpoint')
->assertSee("<input type=\"hidden\" name=\"id\" value=\"expected\">", escape: false);
}
}
If you simply want to test for the presence of text, ignoring the HTML, you can use the
assertSeeText
function instead.
Assert that the given string is contained within the response text. This assertion will automatically escape the given string unless you pass a second argument of
false
. The response content will be passed to thestrip_tags
PHP function before the assertion is made.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.