Cannot verify redirects during Laravel 5 unit testing

Here is my test code:

public function testRegistrationFailsIfNameIsEmpty() { $this->flushSession(); $response = $this->call('POST', '/signup', ['fullname' => '']); $this->assertSessionHasErrors('fullname'); // Passes, expected $this->assertTrue($response->isRedirection()); // Passes, expected $this->assertRedirectedTo('/signup'); // Fails, unexpected. } 

When I call this method, it validates the input, and if the validation fails, it redirects me back to /signup to show validation errors. I checked it manually in the browser and it works as expected.

However, when I run the unit test above, the last statement fails, and it believes that I am redirected only to / rather than /signup .

I have no idea why this is being done. If I verify that the redirect has occurred at all, the test passes because the redirect does occur, it just thinks that the redirect will be / instead of /signup .

I turned off all middleware, so I know that this is not something like guest middleware. I logged in when I do not.

EDIT: test results:

 There was 1 failure: 1) RegistrationTest::testRegistrationFailsIfNameIsEmpty Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'http://localhost/signup' +'http://localhost' 
+8
source share
5 answers

Using request () -> validate () will return you to the calling URL if validation fails. When performing unit tests, there is no calling page, so you will be redirected to the home page.

https://laravel.com/docs/5.5/validation#quick-writing-the-validation-logic

if the check fails, an exception will be thrown and the correct error, the answer will be automatically sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated

Thus, you need to either verify that your redirect to your home or wash your calling URL. Although then you just check the validator and not your code, which actually adds nothing:

 session()->setPreviousUrl('/calling/url'); 

https://www.neontsunami.com/posts/testing-the-redirect-url-in-laravel

Edit: Actually, this can be useful when a test expects a redirect to a route with a named parameter.

+4
source

I had the same problem when testing a Laravel application. It seems that when you use $ this-> call in the test, and then your controller uses something like Redirect :: back () , the redirect is sent to '/'. I believe that this is because you were not on any page when you made "$ this-> call", so the application is not sure where to redirect you, unless you pointed out the redirection in the controller, that is, the redirection ("somePage"),

However, when you do something like $ this-> actAs ($ user) → visit ('somePage') → click ('someButton'), you will be correctly sent to the expected page when using Redirect :: back (). Probably because the application knows which page you started on.

+3
source

I recommend that you print the Response object from the controller using dd () when unit testing this single test. This will show you exactly where the redirect is. The unit test engine simply parses the Response object, and I believe the problem is in the code, not in the Unit test.

0
source

Could you verify that the test hit the redirection function in the controller, you can put a stamp on the function and find out where the error is. This should be a problem in the method. then check if it really is part of the redirect

0
source

You must set → from (route ('RouteName')) to make sure the redirect statement is set correctly

0
source

Source: https://habr.com/ru/post/985087/


All Articles