简体   繁体   English

无法以角度运行Toast消息的单元测试用例

[英]Unable to run unit test case for toast message in angular

Hi I am developing web application in angular 5. I am using toast messages to display messages. 嗨,我正在用角度5开发Web应用程序。我正在使用吐司消息显示消息。 I am using toast messages from https://www.npmjs.com/package/angular2-toaster . 我正在使用来自https://www.npmjs.com/package/angular2-toaster的吐司消息。 The implementation is correct and working fine. 实施是正确的并且可以正常工作。 I am facing issue with writing unit test cases. 我在编写单元测试用例时遇到问题。 Below is my implementation in component. 下面是我在组件中的实现。 I have added below line of code in component. 我在组件中添加了以下代码行。

import { ToasterModule, ToasterService } from 'angular2-toaster';

I have added below code in constructor. 我在构造函数中添加了以下代码。

private toasterService: ToasterService

I am showing toast message as below. 我正在显示如下吐司消息。

this.toasterService.pop('success', 'Args Title', 'Args Body');

I have added below code in HTML file. 我在HTML文件中添加了以下代码。

<toaster-container></toaster-container>

This implementation works fine. 此实现工作正常。 I am writing unit test case as below. 我正在编写如下的单元测试用例。

describe('Component: TenantEditorComponent', () => {

  beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                HttpClientModule,
                RouterTestingModule,
                TranslateModule.forRoot({
                    loader: {
                        provide: TranslateLoader,
                        useClass: TranslateLanguageLoader
                    }
                }),
                NgxDatatableModule,
                FormsModule,
                UiSwitchModule,
                TooltipModule.forRoot(),
                ModalModule.forRoot(),
                ToasterModule
            ],
  providers: [   ToasterService ]
        }).compileComponents();
fixture = TestBed.createComponent(TenantEditorComponent);
        component = fixture.componentInstance;
        submitEl = fixture.debugElement;
        fixture.detectChanges();
    }));

This is giving me error 这给我错误

No Toaster Containers have been initialized to receive toasts. 还没有初始化烤面包机容器来接收烤面包。

I have added screenshot below. 我在下面添加了屏幕截图。

在此处输入图片说明

Can someone help me to figure it out the issue? 有人可以帮我解决问题吗? Any help would be appreciated. 任何帮助,将不胜感激。 Thank you 谢谢

create spy for check the toasterservice. 创建间谍以检查烤面包机服务。

     describe('Component: TenantEditorComponent', () => {
      let toasterServiceSpy: jasmine.Spy;

 const toasterSetvices = jasmine.createSpyObj('toasterSetvices', ['pop']);
     toasterServiceSpy = toasterSetvices.pop.and.returnValue(of(''));

      beforeEach(async(() => {
            TestBed.configureTestingModule({
                imports: [
                    HttpClientModule,
                    RouterTestingModule,
                    TranslateModule.forRoot({
                        loader: {
                            provide: TranslateLoader,
                            useClass: TranslateLanguageLoader
                        }
                    }),
                    NgxDatatableModule,
                    FormsModule,
                    UiSwitchModule,
                    TooltipModule.forRoot(),
                    ModalModule.forRoot(),
                    ToasterModule
                ],
      providers: [    {provide: ToasterService, useValue: toasterSetvices } ]
            }).compileComponents();

    fixture = TestBed.createComponent(TenantEditorComponent);
            component = fixture.componentInstance;
            submitEl = fixture.debugElement;
            fixture.detectChanges();
        }));

   it('Hit the Login Button', () => {
    const fixtureInstance = TestBed.createComponent<LoginComponent> 
     (LoginComponent);
    const toasterServiceInstance = 
    fixtureInstance.componentInstance.toasterService;
    fixtureInstance.detectChanges();
    component.submit();
    expect(toasterServiceSpy.calls.any()).toBe(true);

  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM