1 回答
TA贡献1772条经验 获得超6个赞
我找到了一个名为Runkit的 php 库,但这不是我想要细读的方法。对于手头的问题,感觉很老套和矫枉过正。
函数getAuthClient()定义如下
private function getAuthClient() {
return $this->clients->getClient('auth');
}
由$this->clients构造函数定义
public function __construct(ClientRepo $clients) {
$this->clients = $clients;
}
因此,我模拟ClientRepo并公开了getClient()返回 Mock 的方法AuthClient(无论输入如何),以便我可以控制fetchUserFromToken()调用的返回。
public function testGetUser() {
$client = $this->createMock(WebdevClient::class);
$client->expects($this->any())
->method('fetchUserFromToken')
->will($this->returnCallback(function()
{
// TARGET CODE
}));
$clients = $this->createMock(ClientRegistry::class);
$clients->expects($this->any())
->method('getClient')
->will($this->returnCallback(function() use ($client)
{
return $client;
}));
$object = new TargetObject($clients);
$result = $object->getUser(...);
// ... Assertions to follow
}
- 1 回答
- 0 关注
- 95 浏览
添加回答
举报