我们应该如何编写Laravel中的存储库,逻辑和控制器层的测试?
我正在尝试为存储库,逻辑和控制器层编写测试。注入逻辑和逻辑类中的存储库类注入控制器。我在逻辑类中编写了存储库和模拟存储库的测试,以编写逻辑测试。但是对于控制器,我编写了这样的集成测试:
public function test_create_role()
{
$data = Role::factory()->make()->toArray();
$this->postJson(route('roles.store'), $data)
->assertCreated();
}
我需要检查是否已将新数据添加到这样的数据库中:
public function test_create_role()
{
$data = Role::factory()->make()->toArray();
$this->postJson(route('roles.store'), $data)
->assertCreated();
$this->assertDatabaseHas('roles', $data);
}
还是存储库测试足够? 我们需要分别测试存储库层和逻辑层,还是集成测试足够?
I am trying to write test for repository, logic and controller layer. Repository class injected inside logic and logic class injected to controller. I wrote the test for repository and mock repository in logic class to write test for logic. But for controller i wrote integration test like this:
public function test_create_role()
{
$data = Role::factory()->make()->toArray();
$this->postJson(route('roles.store'), $data)
->assertCreated();
}
Do I need to check if new data has been added to data base like this:
public function test_create_role()
{
$data = Role::factory()->make()->toArray();
$this->postJson(route('roles.store'), $data)
->assertCreated();
$this->assertDatabaseHas('roles', $data);
}
Or is the repository test enough?
Do we need to test the repository layer and logic layer separately, or is integration testing enough?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Laravel使用活动记录,因此没有存储库,您无需测试它,因为它是框架的一部分。只是假设它只是有效的。更多地关注应用逻辑和数据有效性。
Laravel uses active record, so there are no repositories, and you don't need to test it as it is part of the framework. Just assume that it just works. Focus more on app logic and data validity.