sfsccn's avatar

How to test response content is an exact match?

I'm new to Laravel and am tinkering with tests. The following tests for a substring in the response:

$response = $this->get('/hello');
$response->assertSee('Hello World!');

I didn't find a assertion for equality so I'm wondering how to do this. The following seems to work:

$response = $this->get('/hello');
$content = $response->getContent();
$this->assertEquals('Hello World!', $content);

Is this a reasonable approach? I'm skeptical because this is really the result of me hunting through the source code to find what appeared to be an appropriate method. I think it goes like this:

  • $response is in TestResponse
  • TestResponse has a method __get($key) returning $this-baseResponse-{$key}
  • This results in TestResponse having a getContent() method
  • TestResponse::getContent() is derived from the TestReponse::$baseResponse which is in Illuminate\Http\Response
  • Illuminate\Http\Response extends Symfony\Component\HttpFoundation\Response
  • Symfony\Component\HttpFoundation\Response has a getContent() method which returns the string property $content

I'm using

  • laravel: 6.14.0
  • phpunit: 8.5.2
  • php: 7.2.24

Thanks.

0 likes
1 reply

Please or to participate in this conversation.