Summer Sale! All accounts are 50% off this week.

sampang's avatar

Testing

Hello folks, I want to start writing tests for my applications . I Just watched couple of tutorials . My questions might be silly so pardon me. There they are declaring $response variable and assigning value as follows :

	$response = $this->get(route('auth.login'));

what are $response and $this variables in this context ? what they refers to ?

and also i have seen them doing

	$response->assertSomething('something');
	$this->assertSomething();

Why I can't just do like this :

	$this->get(route('route.name'))
		->assertSomething();
0 likes
3 replies
Sinnbeck's avatar

$this refers to the test and the class it extends (laravels test case). $response is just that. A response class with alot of helpers to test the state of the given response.

In theory you should be able to just chain it, but part of testing is breaking it up. It usually have 3 parts

  1. Arrange
  2. Act
  3. Assert

So here calling the endpoint is 2 and asserting is a seperate action, 3.

1 like
MohamedTammam's avatar

what are $response and $this variables in this context ? what they refers to ?

this is the refers to the class itself and I can't explain more in just a comment.

response is the returned value, which you can use to do some tests on the response result.

Why I can't just do like this

You can do it, It's just depends on how you want to write it. It doesn't matter which way.

1 like

Please or to participate in this conversation.