在另一个服务中订阅 EventEmitter angular 单元测试
Subscribe EventEmmiter in an other service angular unit test
我尝试测试 EventEmitter 是否在 angular 服务中正确调用。但是我有两个服务。
服务一
public methodToTestInServiceOne(message: string) {
this.serviceTwo.methodInServiceTwo(message);
}
服务二
public onMessageStart = new EventEmitter();
public onMessageEnd = new EventEmitter();
public methodInServiceTwo(message: string): void {
this.onMessageStart.emit(message);
/* Code... */
this.onMessageEnd.emit(message);
}
单元测试服务一
describe("serviceOne", (): void => {
let serviceOne: ServiceOne;
let serviceTwo: ServiceTwo;
beforeEach((): void => {
TestBed.configureTestingModule({ providers: [] });
service = TestBed.inject(ServiceOne);
serviceTwo = TestBed.inject(ServiceTwo);
});
describe("methodToTestInServiceOne", (): void => {
it("should call EventEmmiter", (): void => {
let isMessageSend: boolean = false;
serviceTwo.onMessageEnd.subscribe((message: string): void => {
isMessageSend = true;
});
serviceOne.methodToTestInServiceOne("Hello World !");
expect(isMessageSend).toBeTruthy();
});
});
});
此代码不起作用,但如果我直接调用我的服务二。
serviceTwo.methodInServiceTwo("Hello World !")
测试有效!
拜托,我如何测试调用 serviceOne 的 EventEmitter?
尝试将 ServiceOne
和 ServiceTwo
放入 TestBed
的 providers
数组中。
试试这个:
describe("serviceOne", (): void => {
let serviceOne: ServiceOne;
let serviceTwo: ServiceTwo;
beforeEach((): void => {
TestBed.configureTestingModule({
providers: [
ServiceOne,
ServiceTwo, // !! - Add these two here - !!
]
});
service = TestBed.inject(ServiceOne);
serviceTwo = TestBed.inject(ServiceTwo);
});
describe("methodToTestInServiceOne", (done): void => { // !! add done here
it("should call EventEmmiter", (): void => {
let isMessageSend: boolean = false;
serviceTwo.onMessageEnd.subscribe((message: string): void => {
console.log(`1.) subscribing to message, ${message}`);
console.log(`2.) Setting isMessageSend to true`);
isMessageSend = true;
done(); // !! call done to let Jasmine know we are done with the test
});
serviceOne.methodToTestInServiceOne("Hello World !");
console.log(`3.) Checking isMessageSend is truthy`);
expect(isMessageSend).toBeTruthy();
});
});
});
public methodToTestInServiceOne(message: string) {
console.log('[Service One] calling method in service two');
this.serviceTwo.methodInServiceTwo(message);
}
....
public methodInServiceTwo(message: string): void {
this.onMessageStart.emit(message);
/* Code... */
console.log('[Service Two] onMessageEnd being emitted');
this.onMessageEnd.emit(message);
}
我尝试测试 EventEmitter 是否在 angular 服务中正确调用。但是我有两个服务。
服务一
public methodToTestInServiceOne(message: string) {
this.serviceTwo.methodInServiceTwo(message);
}
服务二
public onMessageStart = new EventEmitter();
public onMessageEnd = new EventEmitter();
public methodInServiceTwo(message: string): void {
this.onMessageStart.emit(message);
/* Code... */
this.onMessageEnd.emit(message);
}
单元测试服务一
describe("serviceOne", (): void => {
let serviceOne: ServiceOne;
let serviceTwo: ServiceTwo;
beforeEach((): void => {
TestBed.configureTestingModule({ providers: [] });
service = TestBed.inject(ServiceOne);
serviceTwo = TestBed.inject(ServiceTwo);
});
describe("methodToTestInServiceOne", (): void => {
it("should call EventEmmiter", (): void => {
let isMessageSend: boolean = false;
serviceTwo.onMessageEnd.subscribe((message: string): void => {
isMessageSend = true;
});
serviceOne.methodToTestInServiceOne("Hello World !");
expect(isMessageSend).toBeTruthy();
});
});
});
此代码不起作用,但如果我直接调用我的服务二。
serviceTwo.methodInServiceTwo("Hello World !")
测试有效! 拜托,我如何测试调用 serviceOne 的 EventEmitter?
尝试将 ServiceOne
和 ServiceTwo
放入 TestBed
的 providers
数组中。
试试这个:
describe("serviceOne", (): void => {
let serviceOne: ServiceOne;
let serviceTwo: ServiceTwo;
beforeEach((): void => {
TestBed.configureTestingModule({
providers: [
ServiceOne,
ServiceTwo, // !! - Add these two here - !!
]
});
service = TestBed.inject(ServiceOne);
serviceTwo = TestBed.inject(ServiceTwo);
});
describe("methodToTestInServiceOne", (done): void => { // !! add done here
it("should call EventEmmiter", (): void => {
let isMessageSend: boolean = false;
serviceTwo.onMessageEnd.subscribe((message: string): void => {
console.log(`1.) subscribing to message, ${message}`);
console.log(`2.) Setting isMessageSend to true`);
isMessageSend = true;
done(); // !! call done to let Jasmine know we are done with the test
});
serviceOne.methodToTestInServiceOne("Hello World !");
console.log(`3.) Checking isMessageSend is truthy`);
expect(isMessageSend).toBeTruthy();
});
});
});
public methodToTestInServiceOne(message: string) {
console.log('[Service One] calling method in service two');
this.serviceTwo.methodInServiceTwo(message);
}
....
public methodInServiceTwo(message: string): void {
this.onMessageStart.emit(message);
/* Code... */
console.log('[Service Two] onMessageEnd being emitted');
this.onMessageEnd.emit(message);
}