【问题标题】:Compare two objects of different types比较两个不同类型的对象
【发布时间】:2019-09-21 04:32:34
【问题描述】:

我正在尝试为一个函数编写一个测试,该函数接受一个对象并只过滤掉我想要的属性,例如:

const srcObj = {
 singleString: 'this',
 extraString: 'what?',
 singleNumber: 4,
 extraNumber: 0,
 singleObject: {
   prop: 'this'
 },
 extraObject: {
   nope: 'what is this'
  }
};

变成

const expected: Model = {
  singleString: 'this',
  singleNumber: 4,
  singleObject: {
    prop: 'this'
  }
};

当我尝试运行测试时

const modelObj = new Model()
const result = filterFunction(srcObj, modelObj)
expect(result).toEqual(expected)

我得到了错误

    Expected object to be a kind of Object, but was Model({ singleString: 'this', singleNumber: 4, singleObject: Object({  }) }).

我尝试在变量声明中将 srcObj 转换为 Model(我觉得这有点违背目的),在期望语句中将两个对象转换为相同(即 expect(result as Model).toEqual(expected as Model),并将它们都转换为any (expect(result as any).toEqual(expected as any))

我确信我缺少一个相对较小的东西来让这个测试工作,但我不确定它是什么。

编辑

我的filterfunction:

filterFunction(srcObj: any, destObj: any): any {
        var lSrcKeys: string[] = Object.keys(srcObj);
        var lDestKeys: string[] = Object.keys(destObj);
        var lSrcKeyFound: string = '';
        var lKeyType: string = '';
        var lKeyArrayType: string = '';

        lDestKeys.forEach((lDestKey) => {
            lSrcKeyFound = '';

            lSrcKeys.forEach((lSrcKey) => {
                if (lSrcKey.toLowerCase() === lDestKey.toLowerCase()) {
                    lSrcKeyFound = lSrcKey;
                }
            });

            if (lSrcKeyFound !== '') {
                lKeyType = Object.prototype.toString.call(srcObj[lSrcKeyFound]);

                if (lKeyType === '[object Array]' && srcObj[lSrcKeyFound].length) {
                    lKeyArrayType = Object.prototype.toString.call(srcObj[lSrcKeyFound][0]);

                    if (lKeyArrayType === '[object Object]') {
                        for (var i = 0; i < srcObj[lSrcKeyFound].length; i++) {
                            var lSrcArrayItem: any = srcObj[lSrcKeyFound][i];

                            destObj[lDestKey].push(ModelUtil.filterFunction(lSrcArrayItem, null));
                        }
                    } else {
                        destObj[lDestKey] = srcObj[lSrcKeyFound];
                    }
                } else if (lKeyType === '[object Object]') {
                    ModelUtil.filterFunction(srcObj[lSrcKeyFound], destObj[lDestKey]);
                } else {
                    destObj[lDestKey] = srcObj[lSrcKeyFound];
                }
            }
        });
        return destObj;
    }

【问题讨论】:

  • 你能把filterFunction的代码贴出来吗?
  • 有点乱。我实际上是在尝试编写测试,所以当我重构时我有一些东西要检查。不过我会看看我能做些什么。
  • TypeScript 没有大多数人所说的“强制转换”;它有type assertions,运行时影响为零。如果您的问题是在运行时而不是在编译时,那么使用as 的次数不会改变这一点。
  • 我也在使用 Karma 作为我的测试运行器,如果这对任何人都有帮助的话。
  • expect(obj1).toEqual(obj2) 可能会关心 obj1obj2 是否由不同的构造函数创建,而 TypeScript 在确定兼容性时并不关心对象是如何构造的。基本上这不是 TypeScript 问题,而是 JavaScript 问题。

标签: typescript jasmine


【解决方案1】:

看起来像是茉莉花检查构造函数而不是打字稿问题

您可以解构以将它们获取到相同的构造函数

expect({...objOfTypeA}).toEqual({...objOfTypeB});

https://github.com/jasmine/jasmine/issues/598#issuecomment-340284189

【讨论】:

    猜你喜欢
    • 2012-09-07
    • 1970-01-01
    • 2019-08-06
    • 2021-10-08
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    相关资源
    最近更新 更多