Laravel测试的最佳方法是否会改变

发布于 2025-01-21 01:29:24 字数 2525 浏览 2 评论 0原文

我有一个数据库

,该数据库有一张针对团队和讲师的表:

| id | team_id | instructor_id | ... |

每24小时运行一次,以更新该团队的讲师。
我需要创建的是一个简单的测试,以检查批处理是否正确运行,并更改了讲师。

批处理

更新教师随机随机,这意味着同一讲师可能会得到选择两次,在运行测试时丢了错误。我尝试检查UPDATED_AT列是否更改,但是当相同值传递给update() laravel不会更改上述列时。


我应该如何检查讲师是否更改? 我只分配了进行测试,因此我不应该更改批处理操作的方式(例如强制强制,以便更新更改)
但是,直到教练更改为丑陋的循环看起来很丑陋。
而且,由于只有3到5位讲师之间,因此连续选择同一位讲师的机会并不是很低。

更新

此处是根据要求的代码。
测试文件:

class InstructorSelectionOrderTest extends TestCase
{
    use RefreshDatabase;

    ...

    /**
     * @test
     * @group instructorSelection
     *
     * @return void
     */
    public function test_old_team_instructor_random()
    {
        $team = factory(RoutineTeam::class)->create(['instructor_id' => 4, 'updated_at' => Carbon::now()->subDays(1)]);
        $room = factory(Room::class)->create(['team_id' => $team->id]);

        $this->createNewUser($team->id, $room->id);

        $this->artisan('project:reassign_instructors')->assertExitCode(0);

        $result = RoutineTeam::where('id', $team->id)->get();

        $this->assertFalse($result[0]->updated_at == $team->updated_at);
    }

批处理:

class ReassignInstructors extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project:reassign_instructors';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //list of rooms that needs to be updated
        $routineTeams = RoutineTeam::openedRoomTeams();

        ...

        foreach ($routineTeams as $routineTeam) {
            //Instructor::getRandomInstructor()->id = random number from 1 to 4
            $routineTeam->instructor_id = Instructor::getRandomInstructor()->id;
            $routineTeam->save();
        }

        return 0;
    }
}

常规表:

| id | team_id | instructor_id | ... | updated_at

测试当前有25%的失败机会。我只能编辑测试文件。有没有办法只能通过更改测试文件才能使其失败0%?

The code

I have a database, which has a table for Teams and Instructors along the lines of:

| id | team_id | instructor_id | ... |

Every 24 hours a batch runs that updates the instructor for that team.
What I need to create is a simple Test to check if the batch is running properly, and the instructor is changed.

The Issue

The batch updates the instructor randomly, meaning that the same instructor might get chosen twice, throwing an error when running the test. I tried checking if the updated_at column changes, but it seems like when the same values are passed to update() Laravel does not change said column.

The question

How am I supposed to check if the instructor changes?
I am only assigned to making the test, so I should not change the way the batch operates (like forcing it so that updated_at changes)
However making a for loop until the instructor is changed looks very ugly.
And since there are only between 3 to 5 instructors, the chances of the same instructor being picked a few times in a row is not that low.

Update

Here is the code as requested.
The test file:

class InstructorSelectionOrderTest extends TestCase
{
    use RefreshDatabase;

    ...

    /**
     * @test
     * @group instructorSelection
     *
     * @return void
     */
    public function test_old_team_instructor_random()
    {
        $team = factory(RoutineTeam::class)->create(['instructor_id' => 4, 'updated_at' => Carbon::now()->subDays(1)]);
        $room = factory(Room::class)->create(['team_id' => $team->id]);

        $this->createNewUser($team->id, $room->id);

        $this->artisan('project:reassign_instructors')->assertExitCode(0);

        $result = RoutineTeam::where('id', $team->id)->get();

        $this->assertFalse($result[0]->updated_at == $team->updated_at);
    }

The batch:

class ReassignInstructors extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project:reassign_instructors';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //list of rooms that needs to be updated
        $routineTeams = RoutineTeam::openedRoomTeams();

        ...

        foreach ($routineTeams as $routineTeam) {
            //Instructor::getRandomInstructor()->id = random number from 1 to 4
            $routineTeam->instructor_id = Instructor::getRandomInstructor()->id;
            $routineTeam->save();
        }

        return 0;
    }
}

The RoutineTeam table:

| id | team_id | instructor_id | ... | updated_at

The test currently has a 25% chance of failing. I can only edit the test file. Is there a way to make it a 0% chance of failing only by changing the test file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

榕城若虚 2025-01-28 01:29:24

由于讲师 ID 在您的测试中是静态的,因此我猜测它没有外键约束。

那么,为什么不将 coach_id 设置为 0,然后运行作业(或任何不存在的 id)。

 /**
     * @test
     * @group instructorSelection
     *
     * @return void
     */
    public function test_old_team_instructor_random()
    {
        //setting the id to 0 so it is forced to change on 'project:reassign_instructors' command
        $team = factory(RoutineTeam::class)->create(['instructor_id' => 0, 'updated_at' => Carbon::now()->subDays(1)]);
        $room = factory(Room::class)->create(['team_id' => $team->id]);

        $this->createNewUser($team->id, $room->id);

        $this->artisan('project:reassign_instructors')->assertExitCode(0);

        $result = RoutineTeam::where('id', $team->id)->get();

        $this->assertFalse($result[0]->updated_at == $team->updated_at);
    }

Since the instructor id is static in your test, I'm guessing there is no foreign key constraint on it.

So, why not set the instructor_id to 0 and then run the job (or any non existing id).

 /**
     * @test
     * @group instructorSelection
     *
     * @return void
     */
    public function test_old_team_instructor_random()
    {
        //setting the id to 0 so it is forced to change on 'project:reassign_instructors' command
        $team = factory(RoutineTeam::class)->create(['instructor_id' => 0, 'updated_at' => Carbon::now()->subDays(1)]);
        $room = factory(Room::class)->create(['team_id' => $team->id]);

        $this->createNewUser($team->id, $room->id);

        $this->artisan('project:reassign_instructors')->assertExitCode(0);

        $result = RoutineTeam::where('id', $team->id)->get();

        $this->assertFalse($result[0]->updated_at == $team->updated_at);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文