[英]How to run unit test case for toast message in angular service?
我正在使用基於Angular 12 的應用程序。我有一個名為notification
服務的服務,它處理來自ngx-toastr庫的 toast 消息。
這是該服務的樣子:
export class NotificationService {
constructor(private toastr: ToastrService) {}
showSuccess(message: string = 'Success ', note: string = ''): void {
this.toastr.success(message, note);
}
showError(message: string = 'Error ', note: string = 'Try again'): void {
this.toastr.error(message, note, {
timeOut: 3000,
});
}
}
服務方法運行良好,但是當我嘗試為它們實施測試時,出現以下錯誤:
NotificationService should test "showSuccess" method FAILED
Error: <spyOn>: could not find an object to spy upon for success()
這些是測試:
describe('NotificationService', () => {
let notificationService: NotificationService,
httpTestingController: HttpTestingController,
toastrService: ToastrService,
notificationServiceSpy: any;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [CommonModule, HttpClientTestingModule, ToastrModule.forRoot()],
declarations: [],
providers: [{ provide: ToastrService, useValue: toastrService }],
}).compileComponents();
notificationService = TestBed.inject(NotificationService);
httpTestingController = TestBed.inject(HttpTestingController);
toastrService = TestBed.inject(ToastrService);
});
it('should be created', () => {
expect(notificationService).toBeTruthy();
});
it('should test "showSuccess" method', () => {
spyOn(toastrService, 'success').and.callThrough();
});
afterEach(() => {
httpTestingController.verify();
});
});
任何幫助表示贊賞。 謝謝!
您收到該消息是因為當您這樣做時:
providers: [{ provide: ToastrService, useValue: toastrService }],
在那個時間點, toastrService
是未定義的,然后當你做spyOn(toastrService, 'success');
由於toastrService
未定義,因此沒有成功方法可以監視。
我會嘲笑toastrService
。
進行以下更改,注意以!!
開頭的行 .
describe('NotificationService', () => {
let notificationService: NotificationService,
httpTestingController: HttpTestingController,
// !! change this line to this
toastrService: jasmine.SpyObj<ToastrService>,
notificationServiceSpy: any;
beforeEach(async () => {
// !! add a new spy object before each test, now toastrService is not undefined
toastrService = jasmine.createSpyObj<ToastrService>('ToasterService', ['error', 'success']);
await TestBed.configureTestingModule({
imports: [CommonModule, HttpClientTestingModule, ToastrModule.forRoot()],
declarations: [],
providers: [
// !! provide NotificationService to the TestBed module because it is under test
NotificationService,
{ provide: ToastrService, useValue: toastrService }],
}).compileComponents();
notificationService = TestBed.inject(NotificationService);
httpTestingController = TestBed.inject(HttpTestingController);
// !! don't need the below line, we already have access to the spy object
// toastrService = TestBed.inject(ToastrService);
});
it('should be created', () => {
expect(notificationService).toBeTruthy();
});
it('should test "showSuccess" method', () => {
// !! you should not spyOn this anymore, toastrService has methods error
// and success now which are both spies.
// spyOn(toastrService, 'success').and.callThrough();
// !! call the method
service.showSuccess('hello world', 'hello');
expect(toastrService.success).toHaveBeenCalledWith('hello world', 'hello');
});
afterEach(() => {
httpTestingController.verify();
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.