Simple Authntication

Published 8 months ago by lorvent

I am Executing The unit test case for login page. I am getting The following Error. Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'http://localhost/index' +'http://localhost'

This is my Code public function testCorrectValues(){ $response = $this->post('/login',['email'=>'[email protected]','password'=>'123456','_token' => csrf_token()]); $response->assertRedirect('/index'); $response->assertStatus(200); $response->assertSeeText('Dashboard'); } I dont know What I Done mistake. Please Help me.

lostdreamer_nl

You are asserting that there should be a redirect to /index

$response->assertRedirect('/index'); 

but actually, the redirect is to /

Change it to this and it should work


    
    public function testCorrectValues()
    {
        $response = $this->post('/login', ['email' => '[email protected]', 'password' => '123456', '_token' => csrf_token()]);
        $response->assertRedirect('/');
        $response->assertStatus(200);
        $response->assertSeeText('Dashboard');
    } 

lorvent

I used the above code and executed i am getting the error.

  1. Tests\Unit\LoginTest::testCorrectValues Expected status code 200 but received 302. Failed asserting that false is true.
impbob

A normal request will return a 200, a redirect request will return 302.

assertRedirect() checks the status code is 302 and the url matches.

assertStatus() checks the status code.

You're code is recieveing a redirect response so the status is 302, not 200 and therefore failing.

To make it pass, change to assertStatus(302), but since you are already checking the response is a redirect in the line above, its redundate so simply delete it.

Wikipedia has a good list of HTTP status codes with a general description: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

lostdreamer_nl

and you can do:

        $response = $this->post('/login', ['email' => '[email protected]', 'password' => '123456', '_token' => csrf_token()]);
        $response->assertRedirectedTo('/pageWhereYouShouldBeRedirecting');

Which will check to see where you get redirected to.

Please sign in or create an account to participate in this conversation.