【问题标题】:PACT Provider verification : Expected an Array but got a HashPACT 提供者验证:需要一个数组但得到一个哈希
【发布时间】:2019-04-29 09:45:10
【问题描述】:

在消费者层面的测试是:

describe('Listing first level taxonomies', () => {
    before(() => {
        return provider.addInteraction({
            given: 'GET call for first level taxonomies',
            uponReceiving: 'Get object for listing first level 
                            taxonomies',
            withRequest: {
                method: 'GET',
                path: '/api/taxonomies/8061159/taxons?deleted=false',
            },
            willRespondWith: {
                status: 200,
                headers: { 'Content-Type': 'application/json' },
                body: firstChild
            }
        });
    });


const firstChild = eachLike({
'name': like('Geo'),
'taxons': [
    eachLike({
        'id': like(115590),
        'name': like('Africa'),
        'hasChildren':like (false)
    })
  ]
});

当我在提供者级别进行验证时,我收到以下错误: 期望一个数组但得到一个哈希 ({"name"=>"Demo Taxonomy", "taxons"=> [{"id"=>8145188, "name"=>"manojtaxon", "childCategoryName"=>"你好", “有孩子”=>真} 完全错误出现在: https://pastebin.com/XvB17SXY

请帮我解决问题 我认为数组大小更多的是在提供者级别,这就是它的原因 失败了。虽然我已经添加了like和eachLike(仍然没有 解决了)。

【问题讨论】:

    标签: javascript arrays provider pact


    【解决方案1】:

    问题是你的匹配器不正确:

    应该是这样的:

    const firstChild = like({
      'name': 'Geo',
      'taxons': eachLike({
              'id': 115590,
              'name': 'Africa',
              'hasChildren': false
          })
      });                          
    

    注意删除[]

    此外,您可以避免在eachLike 中使用like 语句,因为默认情况下它是隐含的。如果您需要使用不同的匹配器(例如term,那么您可以覆盖该行为,但您似乎只是在这里使用like)。

    【讨论】:

    • 感谢您的回复。它的工作原理是这样的:'name': like('Geo'), 'taxons': eachLike({ 'id': 115590, 'name': 'Africa', 'hasChildren': false }) } 但是,如果我想在诸如 taxon[0],taxon[1] 这样的分类单元上添加更多元素,我该怎么做?我确实喜欢这个 firstChild = { 'name': like('Geo'), 'taxons': eachLike({ 'id': 115590, 'name': 'Africa', 'hasChildren': false },{ 'id' : 115578, 'name': 'Asia', 'childCategoryName': 'Country', 'hasChildren': true }) 但它失败了
    • 这是说它期待一个数组但得到了一个哈希值。如果提供程序返回一个带有 taxons 作为数组的对象,请将第一个 eachLike 更改为 like
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 2019-08-20
    相关资源
    最近更新 更多