【问题标题】:How to dynamically test for typeof array within object?如何动态测试对象内的 typeof 数组?
【发布时间】:2019-04-22 23:50:03
【问题描述】:

我有一个用户对象 - 我想为每个用户属性生成测试并检查它是否是正确的类型。但是,由于 typeof 数组是一个对象,因此数组属性上的断言失败,并显示“AssertionError:预期 [1] 是一个对象”。

因此,我检查了该属性是否是一个数组,然后为它生成特殊测试。我想知道这是否是正确的方法?我有一种感觉,我错过了一些明显的东西。

Object.keys(pureUser).forEach(property =>{
    // since typeof array is an object we need to check this case separately or test will fail with expecting array to be an object
    if (Array.isArray(pureUser[property])) {
         it(`should have property ${property}, type: array`, function () {
             user.should.have.property(property);
           });
         } else {
             it(`should have property ${property}, type: ${(typeof pureUser[property])}`, function () {
                 user.should.have.property(property);
                 user[property].should.be.a(typeof pureUser[property]);
             });
         }
    });

pureUser 是这样的:

let pureUser = {
    username: "JohnDoe123",
    id: 1,
    categories: [1,2,3,4]
}

用户变量通过 got.js 在别处定义

【问题讨论】:

  • 似乎没有定义用户变量。
  • 是的,我在代码的其他位置获取了用户变量。这段代码确实会生成测试并检查类型,但正如我所说,我认为我做错了。
  • 是否可以在您的问题中发布用户变量中的数据?
  • user 变量与 pureUser 完全相同,所以 user = { username: "JohnDoe123", id: 1, categories: [1,2,3,4] }。不同之处在于 pureUser 是硬编码的,用户是从 API 中获取的,并针对预期结果( pureUser )进行测试。

标签: javascript node.js mocha.js chai should.js


【解决方案1】:

将您的测试更改为 pureUser[property].should.be.an.Arrayuser[property].should.be.an.Array

forEach

forEach() 方法按顺序为数组中的每个元素调用一次提供的函数。

let pureUser = {
username: "JohnDoe123",
id: 1,
categories: [1,2,3,4]
}

Object.keys(pureUser).forEach(property =>{

        // since typeof array is an object we need to check this case separately or test will fail with expecting array to be an object
        if (Array.isArray(pureUser[property])) {
        
            console.log('Yes, it\'s an Array')
            //it(`should have property ${property}, type: array`, function () {
            //    user.should.have.property(property);
            //});
        } else {
            console.log('No, it\'s not an Array')
            //it(`should have property ${property}, type: ${(typeof property)}`, function () {
                //user.should.have.property(property);
               // user[property].should.be.a(typeof pureUser[property]);
            //});
        }

    });

当您在 pureUser 上使用 forEach 时,参数将是对象属性,例如 usernameid

let pureUser = {
username: "JohnDoe123",
id: 1,
categories: [1,2,3,4]
}

Object.keys(pureUser).forEach(property =>{
  console.log(property);
});

您还可以在 forEach 函数中访问该数组。

arr.forEach(item, index, arr)

【讨论】:

  • 如果我改变它,我总是有 typeof 返回“字符串”并且测试失败。
  • 该属性永远不会是数组,因此 if 语句永远不会计算为 true
  • 如果属性是数组,其实会返回true。
  • @Junius 感谢您抽出宝贵时间,但不幸的是,这并不能回答我的问题。我的问题是有没有比我发布的更优雅的方法来检查对象中的属性类型。
  • @t-str-os 我认为您应该将测试更改为 pureUser[property].should.be.an.Array 而不是 user.should.have.property(property);
猜你喜欢
  • 1970-01-01
  • 2019-06-20
  • 2022-08-20
  • 2016-06-09
  • 2017-05-13
  • 2012-05-04
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
相关资源
最近更新 更多