【问题标题】:Laravel DatabaseTransactionsLaravel 数据库事务
【发布时间】:2016-02-23 08:23:48
【问题描述】:

我有一个非常奇怪的问题。我正在尝试使用 phpunit 在我的 api 中测试以下函数。

 use DatabaseTransactions;
    public $url = 'test.dev';

    /** @test */
    function show_school_stagiair_authorized()
    {
        factory(Company::class,1)
            ->create(['CompanyId' => 1]);
        factory(School::class,1)
            ->create(['SchoolId' => 1, 'CompanyId' => 1]);
        factory(Mentor::class,1)
            ->create(['MentorId' => 1, 'SchoolId' => 1, 'CompanyId' => 1]);
        factory(Department::class,1)
            ->create(['DepartmentId' => 1,'CompanyId' => 1]);
        factory(Intern::class,1)
            ->create(['InternId' => 1, 'MentorId' => 1]);
        factory(Employee::class,1)
            ->create(['EmployeeId' => 1, 'RoleId' => 1, 'DepartmentId' => 1, 'InternId' => 1, ]);


        $client = new Client();
        $res = $client->request('POST', $this->url.'/api/v1/school/1',[
            'form_params' => [
                'currentUserId' => 1
            ]
        ]);
        $this->assertEquals(200, $res->getStatusCode());
    }

这将返回状态码 500。但是当我删除 use DatabaseTransaction 时它正在工作!?在我的config/database.php 中,我已将默认连接设置为我的测试连接'default' => env('DB_CONNECTION', 'mysql_testing'), 谁能告诉我我做错了什么。

【问题讨论】:

    标签: php unit-testing laravel


    【解决方案1】:

    替换这个:

    use DatabaseTransactions;
    

    与:

    use Illuminate\Foundation\Testing\DatabaseTransactions;
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2020-06-05
      • 2018-11-16
      • 1970-01-01
      • 2018-04-13
      • 2017-11-09
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多