为ng-bootstrap模态(NgbModal)编写单元测试[Angular 6]

时间:2019-12-30 00:28:31

标签: angular unit-testing jasmine karma-jasmine ng-bootstrap

我在为应用程序中存在的确认模式编写单元测试时遇到了麻烦。这是我要测试的代码:

  confirmModal(prompt = 'Are you sure?', title = 'Confirm'): Observable<boolean> {
    const modal = this.ngbModal.open(
      ConfirmModalComponent, { backdrop: 'static' });

    modal.componentInstance.prompt = prompt;
    modal.componentInstance.title = title;

    return from(modal.result).pipe(
      catchError(error => {
        console.warn(error);
        return of(undefined);
      })
    );
  }

有什么建议或例子吗?

1 个答案:

答案 0 :(得分:4)

我根据您的代码段编写了以下测试类:

import { TestBed, ComponentFixture } from '@angular/core/testing';
import { NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ConfirmModalComponent } from './confirm-modal.component';
import { MyComponent } from './test';

// Mock class for NgbModalRef
export class MockNgbModalRef {
    componentInstance = {
        prompt: undefined,
        title: undefined
    };
    result: Promise<any> = new Promise((resolve, reject) => resolve(true));
}

describe('MyComponent tests', () => {

    let fixtureUnderTest: ComponentFixture<MyComponent>;
    let componentUnderTest: MyComponent;
    let ngbModal: NgbModal;
    let mockModalRef: MockNgbModalRef = new MockNgbModalRef();

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [
                MyComponent
            ],
            imports: [
                NgbModule.forRoot()
            ]
        }).compileComponents();

        fixtureUnderTest = TestBed.createComponent(MyComponent);
        componentUnderTest = fixtureUnderTest.componentInstance;
        ngbModal = TestBed.get(NgbModal);
    });

    it('should open modal', () => {
        spyOn(ngbModal, 'open').and.returnValue(mockModalRef);
        componentUnderTest.confirmModal();
        expect(ngbModal.open).toHaveBeenCalledWith(ConfirmModalComponent, { backdrop: 'static' });
    });

    it('should set prompt and title to defaults', () => {
        spyOn(ngbModal, 'open').and.returnValue(mockModalRef);
        componentUnderTest.confirmModal();
        expect(mockModalRef.componentInstance.prompt).toBe('Are you sure?');
        expect(mockModalRef.componentInstance.title).toBe('Confirm');
    });

    it('should return the result of the modal', (done: DoneFn) => {
        spyOn(ngbModal, 'open').and.returnValue(mockModalRef);
        componentUnderTest.confirmModal().subscribe((result: boolean) => {
            expect(result).toBe(true);
            done();
        });
    });

    it('should return undefined if there is an error', (done: DoneFn) => {
        spyOn(ngbModal, 'open').and.returnValue(mockModalRef);
        // Override the result returned from the modal so we can test what happens when the modal is dismissed
        mockModalRef.result = new Promise((resolve, reject) => reject(false));
        componentUnderTest.confirmModal().subscribe((result: boolean) => {
            expect(result).toBeUndefined();
            done();
        });
    });

});

测试如下:

  1. 应打开模式:测试用正确的参数调用了ngbModal.open方法。

  2. 应将prompttitle设置为默认值:测试prompttitle属性是否正确设置为其默认值模态打开后。为此,我必须将以下对象添加到MockNgbModalRef中,以便提示和标题的值可以由组件本身更新。

componentInstance = {
    prompt: undefined,
    title: undefined
};
  1. 应返回模态的结果:测试是否从该方法返回了modal.result的值。随着该方法返回一个Observable,我需要对其进行订阅并在订阅中进行断言。我注入了DoneFn,以便在断言之后调用done()。这意味着如果断言永远不会发生(例如,组件中存在错误),则done()不会被调用,并且测试将失败。

  2. 如果出现错误,应该返回undefined::与#3类似,但是它会检查是否拒绝了模态结果(即存在错误),然后返回undefined