静态 class 方法的部分模拟不起作用
Partial mocking of static class methods not working
我正在尝试在 class 上模拟一个静态方法。但是,如果我调用模拟方法,将不再找到 class 变量。似乎整个 class 都被嘲笑了,而 makePartial()
被忽略了。
我在一个空白的 laravel 项目中创建了一个错误案例。这是相关代码:
另一个控制器:
namespace App\Http\Controllers;
class AnotherController extends Controller
{
public function coolMethod()
{
logger(StaticController::$staticArray);
logger(StaticController::staticMethod('arg1'));
}
}
静态控制器
namespace App\Http\Controllers;
class StaticController extends Controller
{
public static $staticArray = [
'foo',
'bar'
];
public static function staticMethod($arg1, $arg2 = [])
{
logger("The real static method");
logger(self::$staticArray);
}
}
示例测试
namespace Tests\Feature;
use App\Http\Controllers\AnotherController;
use App\Http\Controllers\StaticController;
使用Tests\TestCase;
class ExampleTest extends TestCase
{
public function testStaticMock()
{
$mock = \Mockery::mock('alias:App\Http\Controllers\StaticController');
$mock
->makePartial()
->shouldReceive('staticMethod')
->withAnyArgs()
->andReturn("I'm the mocked return");
$anotherController = new AnotherController();
logger($anotherController->coolMethod());
logger(StaticController::staticMethod());
}
}
输出:
[16:01:24] user@shell [~/Development/Code/Laravel] $ vendor/phpunit/phpunit/phpunit -v
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.14 with Xdebug 2.6.0
Configuration: /Users/.../Development/Code/Laravel/phpunit.xml
E 1 / 1 (100%)
Time: 183 ms, Memory: 12.00MB
There was 1 error:
1) Tests\Feature\ExampleTest::testStaticMock
Error: Access to undeclared static property: App\Http\Controllers\StaticController::$staticArray
/Users/.../Development/Code/Laravel/app/Http/Controllers/AnotherController.php:9
/Users/.../Development/Code/Laravel/tests/Feature/ExampleTest.php:22
ERRORS!
Tests: 1, Assertions: 1, Errors: 1.
如您所见,$staticArray
已找不到,即使它是在原始 class 上定义的。
非常感谢任何帮助!
事实证明,不可能将 makePartial()
与别名模拟一起使用。那是因为 class 被完全替换了:
Prefixing the valid name of a class (which is NOT currently loaded) with “alias:”
will generate an “alias mock”. Alias mocks create a class alias with the given classname
to stdClass and are generally used to enable the mocking of public static methods.
Expectations set on the new mock object which refer to static methods will be used
by all static calls to this class.
可以找到文档here
我正在尝试在 class 上模拟一个静态方法。但是,如果我调用模拟方法,将不再找到 class 变量。似乎整个 class 都被嘲笑了,而 makePartial()
被忽略了。
我在一个空白的 laravel 项目中创建了一个错误案例。这是相关代码:
另一个控制器:
namespace App\Http\Controllers;
class AnotherController extends Controller
{
public function coolMethod()
{
logger(StaticController::$staticArray);
logger(StaticController::staticMethod('arg1'));
}
}
静态控制器
namespace App\Http\Controllers;
class StaticController extends Controller
{
public static $staticArray = [
'foo',
'bar'
];
public static function staticMethod($arg1, $arg2 = [])
{
logger("The real static method");
logger(self::$staticArray);
}
}
示例测试
namespace Tests\Feature;
use App\Http\Controllers\AnotherController;
use App\Http\Controllers\StaticController;
使用Tests\TestCase;
class ExampleTest extends TestCase
{
public function testStaticMock()
{
$mock = \Mockery::mock('alias:App\Http\Controllers\StaticController');
$mock
->makePartial()
->shouldReceive('staticMethod')
->withAnyArgs()
->andReturn("I'm the mocked return");
$anotherController = new AnotherController();
logger($anotherController->coolMethod());
logger(StaticController::staticMethod());
}
}
输出:
[16:01:24] user@shell [~/Development/Code/Laravel] $ vendor/phpunit/phpunit/phpunit -v
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.14 with Xdebug 2.6.0
Configuration: /Users/.../Development/Code/Laravel/phpunit.xml
E 1 / 1 (100%)
Time: 183 ms, Memory: 12.00MB
There was 1 error:
1) Tests\Feature\ExampleTest::testStaticMock
Error: Access to undeclared static property: App\Http\Controllers\StaticController::$staticArray
/Users/.../Development/Code/Laravel/app/Http/Controllers/AnotherController.php:9
/Users/.../Development/Code/Laravel/tests/Feature/ExampleTest.php:22
ERRORS!
Tests: 1, Assertions: 1, Errors: 1.
如您所见,$staticArray
已找不到,即使它是在原始 class 上定义的。
非常感谢任何帮助!
事实证明,不可能将 makePartial()
与别名模拟一起使用。那是因为 class 被完全替换了:
Prefixing the valid name of a class (which is NOT currently loaded) with “alias:”
will generate an “alias mock”. Alias mocks create a class alias with the given classname
to stdClass and are generally used to enable the mocking of public static methods.
Expectations set on the new mock object which refer to static methods will be used
by all static calls to this class.
可以找到文档here