早上好!
我最近创建了一个与第三方 API 集成的 Symfony 2 包。
我使用 Guzzle 和很棒的服务描述来创建客户端并调用 API。
现在我想提高质量并添加一些测试。经过一番阅读后,我开始使用 phpspec 对客户端进行功能测试,并使用 Behat 进行行为测试。
我读过你可以模拟 API 响应,但我不知道如何。尽管 phpspec 内部有一个模拟库(预言),但我找不到任何示例如何模拟预言中的响应。
我创建了我的规范
bin/phpspec desc NewFusion/Bundle/HyperMediaBundle/Service/VehicleService
它在Bundle/spec/
文件夹中创建了我的规范类。
<?php
namespace spec\NewFusion\Bundle\HyperMediaBundle\Service;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class VehicleReferenceDataServiceSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('NewFusion\Bundle\HyperMediaBundle\Service\VehicleReferenceDataService');
}
function it_should_return_make_name() {
$this->getMake(array('makeCode' => 1))->shouldReturn(TRUE);
}
}
这是我希望通过此测试模拟的响应:
HTTP/1.1 200 OK
Cache-Control: no-cache,no-store,must-revalidate
Connection: Keep-Alive
Content-Type: application/vnd.siren+json; charset=UTF-8
日期:2014 年 2 月 12 日 07:43:00 GMT
过期时间:星期四,1970 年 1 月 1 日 00:00:00 GMT,0
Keep-Alive:timeout=15,max=100
Pragma:no-cache
服务器:Jetty (8.yz-SNAPSHOT)
传输编码:分块
通过:1.1 test.local{
“类”:[
“Make”
],
“属性”:{
“makeCode”:“1”,
“名称”:“阿尔法罗密欧”
},
“链接”:[
{
“rel”:[
“self”
],
“href”:“ https://test.local/v1/api/vehiclereferencedata/make/1 ”
},
{“rel”:[
“模型”
],
“href”:“ https://test.本地/制造/1/模型“
}
]
}
我希望有人能让我走上正轨:)