使用phpspec测试Symfony 2与Guzzle API客户端的捆绑

时间:2014-02-17 05:13:09

标签: php unit-testing symfony phpspec

早上好!

我最近创建了一个与第三方API集成的Symfony 2软件包 我使用Guzzle和很棒的服务描述来创建客户端并调用API。

现在我想提高质量并为其添加一些测试。经过一些阅读后,我开始使用phpspec进行客户端的功能测试和Behat的行为 我已经读过您可以模拟API响应,但我无法弄清楚如何。虽然phpspec里面有一个模拟库(预言),但我找不到任何关于如何在预言中模仿答案的例子。

我使用

创建了我的规范
bin/phpspec desc NewFusion/Bundle/HyperMediaBundle/Service/VehicleService

它已在Bundle/spec/文件夹中创建了我的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
  缓存控制:无缓存,无存储,必须重新验证   连接:保持活力
  Content-Type:application / vnd.siren + json;字符集= UTF-8
  日期:2014年2月12日07:43:00 GMT
  到期日:1970年1月1日星期四00:00:00 GMT,0
  保持活跃:超时= 15,最大= 100
  Pragma:no-cache
  服务器:Jetty(8.y.z-SNAPSHOT)
  转移编码:分块
  通过:1.1 test.local

     

{
      “阶级”:[
          “制作”
      ],
      “财产”:{
          “makeCode”:“1”,
          “名字”:“阿尔法罗密欧”
      },
      “链接”:[
          {
              “rel”:[
                  “自我”
              ],
              “href”:“https://test.local/v1/api/vehiclereferencedata/make/1
          },
          {               “rel”:[
                  “模式”
              ],
              “href”:“https://test.local/make/1/model
          }
      ]
  }

我希望有人能让我走上正轨:)。

0 个答案:

没有答案