繁体   English   中英

Laravel 嘲弄 - 这个测试没有执行任何断言

[英]Laravel Mockery - This test did not perform any assertions

我正在尝试创建一个嘲弄单元测试,但它给了我这个错误

这个测试没有执行任何断言

public function testGetById()
{
    $mock = Mockery::mock(PostService::class)->makePartial();
    $mock->shouldReceive('getById')
        ->withSomeOfArgs(1);
        $mock->getById(1);
}

. 通过id获取→这个测试没有执行任何断言\tests\Unit\PostControllerTest:php:30

测试:1 次风险时间:0.32 秒警告:Windows 平台不支持 TTY 模式。

很抱歉,我无法 100% 帮助您,我现在无法设置测试项目来尝试此操作,但您可以尝试:

public function testGetById()
{
    $mock = Mockery::spy(PostService::class);
    // Bind the mock to the Service Container so it gets injected (DI)

    // Execute the code that would run that mock
    $mock->shouldHaveReceived('getById')
        ->withSomeOfArgs(1);
}

如果在所需条件下调用了所需的方法,则间谍允许您进行间谍活动 如果你mock ,你只是在“伪造”它将返回或做什么,如果你spy ,你将能够断言方法发生了什么。

Mockery 官方文档中阅读更多相关信息。

有两种选择。

选项1:

  • 确保你已经添加了一个带有 `Mockery::close(); 的 tearDown 方法;
  • 然后使用任何断言。
  • 如果你真的不需要,使用$this->assertTrue(true);

选项 2:

您还可以扩展Mockery\Adapter\Phpunit\MockeryTestCase而不是通常的PHPUnit\Framework\TestCase 这样,您根本不必添加 phpunit 断言。

暂无
暂无

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

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