我应该使用哪种断言方法来通过 api 存储测试?
我正在尝试传递 testStoreUser() 方法,但在使用哪个断言语句时遇到问题。我使用 API 路由而不是带有哈希的服务器路由的密码。 这是我的 testStoreUser():
public function testStoreUser()
{
//Arrange
$data = [
'data' => [
'name' => 'Testing Name',
'email' => '[email protected]',
'password' => 'Harley81',
],
];
//Act
$response = $this->postJson('/api/users/', $data);
//Assert
$response
->assertStatus(201)
->assertJsonStructure([
'data' => [
'name',
'email',
'password',
]
]);
$this->assertDatabaseHas('users', $data['data']);
}
这是我的 api/用户控制器,特别是存储函数:
/**
* Store a newly created user in storage.
*
* @param Request $request
* @return UserResource
*/
public function store(Request $request): UserResource
{
$validated = $request->validate([
'data' => 'required|array',
'data.name'=> 'required|string',
'data.email'=> 'required|string',
'data.password' => ['required', Password::min(8)->mixedCase()],
]);
$user = new User();
$user->fill($validated['data'])->saveOrFail();
return new UserResource($user);
}
这是我收到的当前错误:
Failed asserting that an array has the key 'password'.
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:256
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:241
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:254
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:795
/var/www/html/tests/Feature/UserApiTest.php:49
I am trying to pass my testStoreUser() method and am having issues with which assert statement to use. The password I am using the API route instead to the server route with hash.
Here is my testStoreUser():
public function testStoreUser()
{
//Arrange
$data = [
'data' => [
'name' => 'Testing Name',
'email' => '[email protected]',
'password' => 'Harley81',
],
];
//Act
$response = $this->postJson('/api/users/', $data);
//Assert
$response
->assertStatus(201)
->assertJsonStructure([
'data' => [
'name',
'email',
'password',
]
]);
$this->assertDatabaseHas('users', $data['data']);
}
Here is my api/user controller specifically the store function:
/**
* Store a newly created user in storage.
*
* @param Request $request
* @return UserResource
*/
public function store(Request $request): UserResource
{
$validated = $request->validate([
'data' => 'required|array',
'data.name'=> 'required|string',
'data.email'=> 'required|string',
'data.password' => ['required', Password::min(8)->mixedCase()],
]);
$user = new User();
$user->fill($validated['data'])->saveOrFail();
return new UserResource($user);
}
And this is current error I am getting:
Failed asserting that an array has the key 'password'.
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:256
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:241
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:254
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:795
/var/www/html/tests/Feature/UserApiTest.php:49
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在测试中只需要这个。
I just needed this in my test.