繁体   English   中英

如何在单元测试中模拟方法的结果?

[英]How can I mock the result of a method in my unit test?

我正在学习 OOP 和干净的编码实践,为了做到这一点,我开始在 Laravel 中构建一个基于网络浏览器的游戏。

我有下面的 class 为我提供了一个随机数来知道一个动作是否成功:

class GenericDice
{
    /**
     * 
     * @param int $min
     * @param int $max
     * @return int
     */
    public function random(int $min = 1, int $max = 100) : int
    {
        return mt_rand($min, $max);
    }
}

现在在我的测试中,我想检查当GenericDice::random()返回 100 时,这意味着我必须将操作视为严重失败。

我阅读了Laravel Z0D7794B8F1C7BE8D4A1C19AA38EF329CDZ DOCS,DOCS,嘲笑文档,告诉我不告诉GenericDice Za81259Cef8e9595959C6224DD1D1D1D1D1D1D1D.NED MYNEN MYNY MANTED MYNED MYAND MYENE MYNED MENENEN DONED MYNED MYNED MYDISN D.D1DD。一个固定的结果。

这是测试:

    public function test_character_should_lose_health_when_action_is_critical_failure()
    {
        $this->mock(GenericDice::class, function (MockInterface $mock) {
            $mock->shouldReceive("random")
                ->andReturn(100);
        });

        $character = Character::factory()->create([
            "current_health" => 50,
        ]);

        $response = $this->actingAs($character->user)->put(route("game.action.run"), [
            "ordre_id" => $this->cookAction->id,
        ]);

        $character->refresh();

        $this->assertLessThan(50, $character->current_health);
    }

运行该操作的代码如下所示:

class CookAction extends AbstractAction 
{
    public function run(): bool
    {
        // Let's roll baby...
        $this->diceResult = $this->throwDice();

        // How did I do?
        $this->checkForSuccess();

        // The cooking part below.
    }
}

// Below the AbstractAction
abstract class AbstractAction
{
    protected function throwDice(int $min = 1, int $max = 100): int
    {
        return (new GenericDice)->random($min, $max);
    }
}

我究竟做错了什么?

看起来您正在正确创建模拟,但您没有将模拟绑定到服务容器。 您正在手动创建一个新的GenericDice ,而不是让服务容器解析它。 创建模拟并将其绑定到您的服务容器,然后让应用程序为您解析实例GenericeDice实例。

尝试这个

public function test_character_should_lose_health_when_action_is_critical_failure()
{
    $diceMock = \Mockery::mock(GenericDice::class)->makePartial();
    $diceMock->shouldReceive('random')->andReturn(100);

    // Bind to service container
     $this->app->bind(
        GenericDice::class,
        function () use ($diceMock) {
            return $diceMock;
        }
    );

    $character = Character::factory()->create([
        "current_health" => 50,
    ]);

    $response = $this->actingAs($character->user)->put(route("game.action.run"), [
        "ordre_id" => $this->cookAction->id,
    ]);

    $character->refresh();

    $this->assertLessThan(50, $character->current_health);
}

和这个

abstract class AbstractAction
{
    protected function throwDice(int $min = 1, int $max = 100): int
    {
          // Get the GenericeDice instance from the service container
          $dice = app(GenericDice::class);
          return $dice->random($min, $max);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM