首页 > 解决方案 > chai 无法比较类类型

问题描述

我正在使用 chai 来比较 SimpleModel 类型的返回值是否属于 SimpleModel 类型。尽管我的 IDE 确认类型匹配,但它给了我这个错误:

AssertionError: expected {} to be a simplemodel

这是我的简单设置:

export class SimpleModel {
    public test_property: string;
}

然后我像这样在我的测试中使用它:

it('(Simple Value)"', () => {
   const simpleModel: SimpleModel = new SimpleModel();

   expect(simpleModel).to.be.a('SimpleModel'); //ERROR HAPPENS HERE
});

再次 simpleModel 是,我的 IDE 确认了这一点,类型为SimpleModel. 我不确定为什么 Chai 也会在错误中转换('SimpleModel')为。'simplemodel'

标签: typescriptunit-testingchai

解决方案


a测试typeofor [Symbol.toStringTag]。您必须使用instanceof.


推荐阅读