phpunit测试中的POST,响应和assertJson

问题描述 投票:0回答:1

我具有以下测试功能,以检查更新数据是否正确。更新没有问题。我的问题是更新后如何检查给定的参数是否正确。

例如

if response.id == 1 and response.name = 'Mr.Smith'
    assertcode = OK
else 
    assertcode = NG
public function user_update_info(){
        $this->post('login',['email' => config('my-app.test_user'),
            'password' => config('my-app.test_pass')]);
        $response = $this->post('/update_info',[
            'id'                => 1,
            'name'              => 'Mr.Smith',
            'post_code'         => '142-4756',
            'prefectural_code'  => '15',
            'address'           => 'Merchat St.',]);
        $response->assertStatus(200);
    }
laravel phpunit-testing
1个回答
0
投票

假设您的update_info路线更新User模型。

请在代码后尝试以下内容,

$user = User::find(1);
static::assertTrue($user->id == 1 && $user->name = 'Mr.Smith');

要检查响应是否返回您期望的json数据,可以使用响应对象的assertJson()方法,如下所示:

$response->assertJson([
    'id' => 1,
    'name' => 'Mr.Smith'
]);
© www.soinside.com 2019 - 2024. All rights reserved.