我正在尝试为一个函数做一个测试,该函数接受一个对象并仅过滤出我想要的属性,例如:
Random r = new Random();
long start = 1_000_000_000;
long end = 10_000_000_000L;
long n = r.longs(1, start, end).reduce(0, (a, b) -> b);
String s = String.format("%,d", n).replace(",", "-");
System.out.println(s);
成为
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)
我尝试在变量声明中将srcObj强制转换为 Expected object to be a kind of Object, but was Model({ singleString: 'this', singleNumber: 4, singleObject: Object({ }) }).
(我觉得这有点违反目的),在Expect语句中将两个对象都强制转换为相同的对象(即Model
),并且将它们都投射为expect(result as Model).toEqual(expected as Model)
(any
)
我敢肯定,要使此测试正常工作,我缺少相对较小的东西,但是我不确定它是什么。
修改
我的expect(result as any).toEqual(expected as any)
:
filterfunction
答案 0 :(得分:0)
看起来是茉莉花,检查构造函数,而不是打字稿问题
您可以解构以将它们添加到相同的构造函数中
expect({...objOfTypeA}).toEqual({...objOfTypeB});
https://github.com/jasmine/jasmine/issues/598#issuecomment-340284189