2

I'm learning both Laravel and UnitTesting at the moment, so this may be a stupid question.

I'm getting stuck on how to best test the controller function below:

UserController:

public function store()
{
    $input = Input::all();
    $user = new User($input);

    if( ! $user->save()){
        return Redirect::back()->withInput()->withErrors($user->getErrors());
    }

    return Redirect::to('/user');
}

here's the test as I have it so far:

    /**
 * @dataProvider providerTestUserStoreAddsUsersCorrectly
 */
public function testUserStoreAddsUsersCorrectly($first_name, $last_name, $email, $password)
{
    $response = $this->call('POST', 'user', array('first_name'=>$first_name, 'last_name'=>$last_name, 'email'=>$email, 'password'=>$password));
}

public function providerTestUserStoreAddsUsersCorrectly(){
    return array(
        array("FirstName", "LastName", "[email protected]", "pass1234")
    );

}

This is actually working and adding the user to the db correctly, but I'm not sure how to test the output / what assertions to use as the response should be to add the user to the db and to redirect to the /user page.

How do I finish this test?

1 Answer 1

2

If you need to check success status then you can simply send status code from your controller and check status in test

public function store()
{
    $input = Input::all();
    $user = new User($input);

    if( !$user->save() ){
        return array("status"=>'failed');
    }

    return array("status"=>'success');
}

public function testUserStoreAddsUsersCorrectly($first_name, $last_name, $email, $password)
{
    $requested_arr = [
        'first_name' => $first_name,
        'last_name' => $last_name,
        'email' => $email,
        'password' => $password
    ];
    $response = $this->call('POST', 'user', $requested_arr);
    $data = json_decode($response ->getContent(), true);

    if ($data['status']) {
        $this->assertTrue(true);
    } else {
        $this->assertTrue(false);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.