首页 > 解决方案 > 结合两个对单个 json 响应执行检查的期望语句

问题描述

我正在编写一个测试来检查我的 Node.js 应用程序中返回此JSON结构的函数:

    }
     id: 1,
     name: 'John Doe',
     email: 'j@doe.com',
     phone: '+123',
     suppliers: [ 
       { 
         id: 1, 
         name: 'Supplier1' 
       } 
     ] 
   }

我有这个expect

    expect(res.body.users[0]).to.be.an.instanceof(Object)
      .that.includes.all.keys([
        'id',
        'name',
        'suppliers',
      ]);

我还想查看 中是否有详细信息suppliers。我可以在另一个中添加这个expect

    expect(res.body.users[0].suppliers[0]).to.be.an.instanceof(Object)
      .that.includes.all.keys([
        'id',
        'name',
      ]);

是否可以将两者合并为一个expect语句?

标签: javascriptnode.jstestingmocha.js

解决方案


推荐阅读