问题描述
我正在使用 Jasmine 创建我的单元测试,我对所涵盖的分支有疑问.
有谁知道为什么代码部分显示没有覆盖分支,如下所示?
这是单元测试:
describe('MyComponent', () => {让组件:我的组件;让fixture: ComponentFixture;让我的服务:我的服务;beforeEach(异步(() => {TestBed.configureTestingModule({声明:[我的组件],进口:[ MaterializeModule, FormsModule, ReactiveFormsModule, HttpModule ],提供者:[我的服务,表单生成器]}).compileComponents();}));beforeEach(() => {夹具 = TestBed.createComponent(MyComponent);组件=fixture.componentInstance;slotService = TestBed.get(MyService);夹具.detectChanges();});功能更新表格(姓名,姓氏){component.myForm.controls['name'].setValue(name);component.myForm.controls['surname'].setValue(name);}it('应该创建', () => {期望(组件).toBeTruthy();});}
从我将项目升级到 Angular 4 的那一刻起,几个月来我一直遇到同样的问题.不幸的是,这是 angular-cli 版本 1 和角度 4.
此错误已记录在
I am creating my unit tests with Jasmine and I have a question about the branch covered.
Does anyone know why the code part shows that the branches are not covered as we can see below?
This is the unit test:
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
let myService: MyService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent ],
imports: [ MaterializeModule, FormsModule, ReactiveFormsModule, HttpModule ],
providers: [
MyService,
FormBuilder
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
slotsService = TestBed.get(MyService);
fixture.detectChanges();
});
function updateForm(name, surname) {
component.myForm.controls['name'].setValue(name);
component.myForm.controls['surname'].setValue(name);
}
it('should create', () => {
expect(component).toBeTruthy();
});
}
I've had the same issue for months from the moment I upgraded my projects to angular 4. Unfortunately it is a bug with the angular-cli version 1 and angular 4.
This bug is already logged in the angular-cli project: Test-coverage says that coverage is not 100%, but it truly is! #5871. Unfortunately at the moment, this issue is still open.
This is a snapshot from that logged issue, which matches the one you are asking about:
这篇关于未涵盖构造函数上的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!