我们应该如何编写Laravel中的存储库,逻辑和控制器层的测试?

发布于 2025-02-01 18:30:58 字数 627 浏览 1 评论 0原文

我正在尝试为存储库,逻辑和控制器层编写测试。注入逻辑和逻辑类中的存储库类注入控制器。我在逻辑类中编写了存储库和模拟存储库的测试,以编写逻辑测试。但是对于控制器,我编写了这样的集成测试:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡莣 2025-02-08 18:30:58

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文