1 回答

TA贡献1826条经验 获得超6个赞
作为最佳实践的一部分,我建议进行一些更改。
不要模拟组件方法,因为您需要测试它们。在您的情况下,您应该设置 value
this.service.Type.description
,因此它应该返回true
orfalse
。这将是一个正确的做法。
如果this.service
是已注入的服务construtor
,则可以模拟该服务。
由于您正在使用 测试多个条件
if
else
,因此您需要编写几个it
块才能获得良好的测试覆盖率。要进行测试
var r
,您应该将其声明public
为组件级别的变量,而不是在function
.let
也更喜欢var
.
这是一个示例代码,您可以编写该代码来设置其中的值isBrilliant()
it('should push Brilliant when the Description is so,()=>{
component.service.Type.description = 'Brilliant';
component.onExportToExcel();
expect(component.r.length).toBe(1);
expect(component.r[0]).toBe('This is Brilliant');
})
it('should push other cool content when the Description is not Brilliant,()=>{
component.service.Type.description = 'something else';
component.onExportToExcel();
expect(component.r.length).toBe(2);
// check other values in expect block accordingly
})
// you should also check that "component.r" has zero length when hasDescription() returns false
我希望上面的代码片段能给你一个好的开始
添加回答
举报