简体   繁体   English

如何在 codeception yii2 测试中使用 PhpUnit mocking 方法

[英]How to use PhpUnit mocking methods within codeception yii2 tests

I need to use PHPUnit mocking methods like expect($this->once())->returnSelf();我需要使用 PHPUnit mocking 这样的方法expect($this->once())->returnSelf(); in codecept/yii2 tests.在 codecept/yii2 测试中。 Cannot find any way to call them.找不到任何方式给他们打电话。 The Stub library of codeception does not have as many way to mock a method. codeception 的存根库没有那么多方法来模拟方法。

Although phpunit is wrapped by codeception I neither can find a way to use phpunit with yii 2 for API testing using sendPost etc.尽管 phpunit 被解码包装,但我也找不到使用 phpunit 和 yii 2 进行 API 测试的方法,使用sendPost等。

Please suggest.请建议。

The Codeception\Test\Unit class indirectly extends PHPUnit\Framework\TestCase . Codeception\Test\Unit class 间接扩展了PHPUnit\Framework\TestCase You should be able to use PHPUnit way of creating mock object and then you can set up its methods:您应该能够使用 PHPUnit 创建模拟 object 的方式,然后您可以设置其方法:

public function testMyTestCase()
{
    $mockedObject = $this->createMock(MockedClassOrInterface::class);
    $mockedObject->expects($this->once())
        ->method('methodName')
        ->willReturnSelf();
    // ... rest of the test case code
}

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

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