dimanche 3 janvier 2016

Testing Forms in Laravel

Consider the following test for testing a login controller action:

public function testLoginWithWrongData() {
    $response = $this->action('POST', 'BlogController@postLogin', ['email' => 'jesus@jesus.com', 'password' => 'asdasdasd']);
    $this->assertResponseStatus('302');
}

This works because I do redirect you back if I cannot authenticate you. How ever, I also redirect you if I do authenticate you ...

So ....

In rails I would test things like, redirected back to login form or redirect to dashboard.

The controller action for this test looks like:

public function postLogin(Request $request) {
    $this->validate($request, [
        'email'    => 'required|email',
        'password' => 'required|min:3'
    ]);

    $credentials = $this->getCredentials($request);

    if (Auth::attempt($credentials)) {
        Session::flash('success', "Welcome back Adam. Care to manage your blogs?");
        return redirect()->route('blogs');
    } else {
        Session::flash('error', "I'm sorry. Who are you? I don't recognize you.");
        return redirect()->back();
    }
}

So what test methods do I have to give this test a bit more substance ... instead of here some incorrect or correct details, by the way redirect ...

Aucun commentaire:

Enregistrer un commentaire