Ionic 3 spyOn NavController弹出

时间:2018-08-13 02:06:10

标签: javascript unit-testing ionic-framework jasmine ionic3

我正在为最后一次调用Navcontroller.pop()的方法编写单元测试,但我收到“预期的间谍弹出消息已被调用”。但是该方法被调用

这是我的规格文件

describe('ExpensePage', () => {

  let de: DebugElement;
  let comp: ExpensePage;
  let fixture: ComponentFixture<ExpensePage>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ExpensePage],
      imports: [
        IonicModule.forRoot(ExpensePage)
      ],
      providers: [
        FormBuilder,
        ModalController,
        AlertController,
        { provide: NavController, useClass: NavMock },
        { provide: NavParams, useClass: NavParamsMock },
        { provide: ViewController, useClass: ViewControllerMock },
        { provide: PouchDbProvider, useClass: PouchDbMock },
        { provide: AwsConnectorProvider, useClass: AwsConnectorMock }
      ]
    });
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ExpensePage);
    comp = fixture.componentInstance;
  });
  afterEach(() => {
    fixture.destroy();
    comp = null;
  });

  it('should go to the previous page after saving', () => {
    let navCtrl=fixture.debugElement.injector.get(NavController);
    spyOn(navCtrl, 'pop').and.callThrough();
    let expense = {
      tipoexpense: '1',
      ammount: 3
    };
    comp.trabajo=[];
    comp.expenseForm.patchValue(expense);
    comp.save()
    expect(navCtrl.pop).toHaveBeenCalled();
  });
});

这是我的模拟导航控制器

export class NavMock {

  public pop(): any {
    return new Promise(function (resolve: Function): void {
      resolve();
    });
  }
}

0 个答案:

没有答案