我正在尝试使用带有 phpspec 的 TDD 为 API 类创建一个包装器。
我编写了一个Client
类,它处理从 REST API 请求/检索数据,然后将其映射到几个Entity
类,因此它的行为类似于 ORM。
我现在有点卡住了,我来测试和使用 TDD 扩展应用程序。由于Client
是实体类的依赖项(以便他们可以请求自己的子对象),我正在努力模拟它。
例如,以下是其中一个实体Comic.php
, 可能的样子:
class Comic {
protected $client;
public $id;
public function __construct(Client $client)
{
$this->client = $client;
}
public function getCharacters()
{
// just an example, this would return an array of Character objects
return $this->client->request("comic/{$this->id}/characters");
}
}
为简洁起见,这里是简化版的Client.php
样子:
class Client {
public function __construct($publicKey, $privateKey)
{
// make token from $publicKey, $privateKey
}
public function request($endpoint)
{
// use token for cURL request to endpoint and return data
}
}
那么作为一个例子,一个测试看起来ComicSpec.php
如何呢?it_gets_all_characters()
希望这是有道理的,如果需要可以提供更多信息。
谢谢参观。