Laravel DatabaseTransactions

时间:2016-02-23 08:23:48

标签: php unit-testing laravel

我有一个非常奇怪的问题。我正在尝试使用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'),任何人都可以告诉我我做错了什么。

1 个答案:

答案 0 :(得分:0)

替换这个:

use DatabaseTransactions;

使用:

use Illuminate\Foundation\Testing\DatabaseTransactions;