单元测试动态方法调用 Angular
Unit test dynamic method call Angular
我的组件中有这个服务,其中一个方法是动态调用的
@Input methodName: string;
constructor(
private readonly testService: testService
) {
}
loadData() {
this.testService[this.methodName]().subscribe();
}
我
我在测试方法 loadData() 时遇到了很多麻烦,它一直告诉我 this.testService[methodName] 不是函数,您将如何测试这种方法?
it('should ', fakeAsync(() => {
// getData is the supposed methodName that has to be set;
testService.getData.and.returnValue(asyncData({} as any));
component.loadData();
flush();
expect(testService.getData).toHaveBeenCalled();
}));
在组件构造函数中创建服务public
以便更好地测试
按照此示例作为标准做法:
service.ts
@Injectable({
providedIn: 'root',
})
export class UserSvcService {
test() {
return of('test');
}
}
对于 component.ts
@Component({
selector: 'app-user-detail',
templateUrl: './user-detail.component.html',
styleUrls: ['./user-detail.component.scss'],
})
export class UserDetailComponent implements OnInit {
name = 'test';
val :string
public constructor(public _userSvc: UserSvcService) {}
ngOnInit() {
this._userSvc[this.name]().subscribe(res =>
this.val = res
);
}
}
像这样创建存根:
export class UserServiceStub {
test() {
return of('test mock');
}
}
用存根替换实际服务为:
describe('UserDetailComponent', () => {
let component: UserDetailComponent;
let fixture: ComponentFixture<UserDetailComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [],
declarations: [UserDetailComponent],
providers: [{ provide: UserSvcService, useClass: UserServiceStub }],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserDetailComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
expect(component.val).toBe('test mock'); // move in another "it" block
});
这应该有效。要开始工作,您需要创建一个 spyOn
服务为 public
1st:
it('should ', () => {
spyOn(component.testService,'getData').and.returnValue(return of({some val}));
component.loadData();
expect(component.testService.getData).toHaveBeenCalled();
});
这个 article 您可能更感兴趣
我的组件中有这个服务,其中一个方法是动态调用的
@Input methodName: string;
constructor(
private readonly testService: testService
) {
}
loadData() {
this.testService[this.methodName]().subscribe();
}
我
我在测试方法 loadData() 时遇到了很多麻烦,它一直告诉我 this.testService[methodName] 不是函数,您将如何测试这种方法?
it('should ', fakeAsync(() => {
// getData is the supposed methodName that has to be set;
testService.getData.and.returnValue(asyncData({} as any));
component.loadData();
flush();
expect(testService.getData).toHaveBeenCalled();
}));
在组件构造函数中创建服务
public
以便更好地测试按照此示例作为标准做法:
service.ts
@Injectable({
providedIn: 'root',
})
export class UserSvcService {
test() {
return of('test');
}
}
对于 component.ts
@Component({
selector: 'app-user-detail',
templateUrl: './user-detail.component.html',
styleUrls: ['./user-detail.component.scss'],
})
export class UserDetailComponent implements OnInit {
name = 'test';
val :string
public constructor(public _userSvc: UserSvcService) {}
ngOnInit() {
this._userSvc[this.name]().subscribe(res =>
this.val = res
);
}
}
像这样创建存根:
export class UserServiceStub {
test() {
return of('test mock');
}
}
用存根替换实际服务为:
describe('UserDetailComponent', () => {
let component: UserDetailComponent;
let fixture: ComponentFixture<UserDetailComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [],
declarations: [UserDetailComponent],
providers: [{ provide: UserSvcService, useClass: UserServiceStub }],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserDetailComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
expect(component.val).toBe('test mock'); // move in another "it" block
});
这应该有效。要开始工作,您需要创建一个 spyOn
服务为 public
1st:
it('should ', () => {
spyOn(component.testService,'getData').and.returnValue(return of({some val}));
component.loadData();
expect(component.testService.getData).toHaveBeenCalled();
});
这个 article 您可能更感兴趣