Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 4 使用phpunit测试用户::store()_Laravel 4_Phpunit - Fatal编程技术网

Laravel 4 使用phpunit测试用户::store()

Laravel 4 使用phpunit测试用户::store(),laravel-4,phpunit,Laravel 4,Phpunit,我现在正在学习Laravel和单元测试,所以这可能是一个愚蠢的问题 我一直在思考如何最好地测试以下控制器功能: 用户控制器: public function store() { $input = Input::all(); $user = new User($input); if( ! $user->save()){ return Redirect::back()->withInput()->withErrors($user->g

我现在正在学习Laravel和单元测试,所以这可能是一个愚蠢的问题

我一直在思考如何最好地测试以下控制器功能:

用户控制器:

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

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

    return Redirect::to('/user');
}
    /**
 * @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@add.com", "pass1234")
    );

}
这是我目前的测试:

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

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

    return Redirect::to('/user');
}
    /**
 * @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@add.com", "pass1234")
    );

}
这实际上是可行的,并且正确地将用户添加到数据库中,但是我不确定如何测试输出/使用什么断言作为响应,将用户添加到数据库中并重定向到
/user
页面


我如何完成这个测试

如果需要检查成功状态,则只需从控制器发送状态代码即可 并在测试中检查状态

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);
    }
}