我有一个angular 4应用程序,我使用VS代码,并且我已经开始编写我的第一个单元测试:
describe('DeclarationUtilService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [DeclarationUtilService]
});
this.accident = new Accident();
this.injuredPerson = new InjuredPerson();
this.injuredPerson.lesionNature = new LesionNature();
this.injuredPerson.personType = new PersonType();
this.injuredPerson.personType.id = 1;
this.accident.injuredPersons.push(this.injuredPerson);
});
it('#refreshDeclarationCategorization should return FR0 true for internal', inject([DeclarationUtilService], (service: DeclarationUtilService) => {
this.injuredPerson.personType.internal = true;
const declarationCateg = service.refreshDeclarationCategorization(this.accident);
expect(declarationCateg.fr0)
.toBe(true, 'should be FR0');
}));
我试图在计划在每个测试中使用的“ beforeEach”部分中设置一些变量。测试正常,但是当我尝试使用在beforeEach中设置的变量时,自动补全功能不起作用,这不方便... 我会在某处错过什么吗?还是VS Code的错误?
Thx
答案 0 :(得分:0)
理想情况下,您应该创建要测试的组件的实例,并避免使用this
。
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LoginComponent } from './login.component';
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
然后根据您的情况,它就像-
component.injuredPerson.personType.internal = true;
此外,您无需使用new
创建实例,只需使用TestBed本身添加提供程序即可。