0

我有一些针对我的 REST API 的验收测试,它们基本上都是相同的模式:

<?php
$I = new WebGuy($scenario);
$I->wantTo('Do something');
$I->haveHttpHeader('Accept', 'application/hal+json');
$I->sendGET('contact/1/website');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(array(...

我的一些测试中的问题出现在seeResponseContainsJson。给定一个 JSON 响应:

{
    _links: {
        self: {
            href: "http://myurl/api/v1/contact/1/website"
        }
        describedBy: {
            href: "http://myurl/api/v1/documentation/collection"
        }
    }
    _embedded: {
        items: [1]
            0:  {
                id: 1
                label: "Personal Site"
                url: "http://someurl.com"
                _links: {
                    self: {
                        href: "http://myurl/api/v1/contact/1/website/1"
                    }
                }
            }
    }
}

尝试使用 seeResponseContainsJson 测试响应格式时,我有以下内容:

$I->seeResponseContainsJson(array(
    '_links' => array(
        'self' => array(
            'href' => $baseUrl . 'contact/1/website'
        ),
        'describedBy' => array(
            'href' => $baseUrl . 'documentation/collection' 
        ),      
    ), 
    '_embedded' => array(

    ),
));

问题是这总是失败。但是,如果我删除 _embedded 的检查:

$I->seeResponseContainsJson(array(
    '_links' => array(
        'self' => array(
            'href' => $baseUrl . 'contact/1/website'
        ),
        'describedBy' => array(
            'href' => $baseUrl . 'documentation/collection' 
        ),      
    ), 
));

一切正常。据我所知,响应是相同的,并且似乎没有任何证据表明为什么 _embedded 会导致 JSON 解析崩溃。有任何想法吗?

4

1 回答 1

0

似乎 seeResponseContainsJson 不处理空数组。如果我进一步实现我断言的响应,直到没有空数组:

$I->seeResponseContainsJson(array(
    '_links' => array(
        'self' => array(
            'href' => $baseUrl . 'contact/1/website'
        ),
        'describedBy' => array(
            'href' => $baseUrl . 'documentation/collection' 
        ),      
    ), 
    '_embedded' => array(
        'items' => array(
            array(
                'id' => 1,
            ),
        ),
    ),
));

我的测试现在通过了。现在这已经足够了,但也许处理空数组是未来的功能请求?

于 2014-02-03T18:22:34.777 回答