角度测试错误:预期的间谍getServicingContent已被调用

时间:2018-11-28 15:59:03

标签: angular unit-testing karma-jasmine

这是我的.spec文件。服务内容。我在“ SimonTest”插件的帮助下生成了此文件。我的第一个测试通过了,但是第二个测试ngOnInit makes expected calls失败了。这是我的代码。

 // servicing.component.spec.ts

  import { ComponentFixture, TestBed } from '@angular/core/testing';
  import { NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
  import { ServicingService } from './servicing.service';
  import { Router } from '@angular/router';
  import { ActivatedRoute } from '@angular/router';
  import { StorageService } from 'angular-nexus-uiux/services';
  import { BrandingService } from '/branding.service';
  import { ServicingComponent } from './servicing.component';

    fdescribe('ServicingComponent', () => {

     let comp: ServicingComponent;
     let fixture: ComponentFixture<ServicingComponent>;

      beforeEach(() => {
      const servicingServiceStub = {
        tagSiteCatalyst: () => ({ }),

        getServicingContent: () => ({
            subscribe: () => ({
            })
        })
    };
    const routerStub = {
        events: {
            filter: () => ({
                subscribe: () => ({})
            })
        },
        url: {
            split: () => ({})
        }
    };
    const activatedRouteStub = {
        data: {
            subscribe: () => ({})
        }
    };
    const storageServiceStub = {
        get: () => ({})
    };
    const brandingServiceStub = {};
    TestBed.configureTestingModule({
        declarations: [ ServicingComponent ],
        schemas: [ NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA ],
        providers: [
            { provide: ServicingService, useValue: servicingServiceStub },
            { provide: Router, useValue: routerStub },
            { provide: ActivatedRoute, useValue: activatedRouteStub },
            { provide: StorageService, useValue: storageServiceStub },
            { provide: BrandingService, useValue: brandingServiceStub }
        ]
    });
    fixture = TestBed.createComponent(ServicingComponent);
    comp = fixture.componentInstance;
});

it('can load instance', () => {
    expect(comp).toBeTruthy();
});

describe('ngOnInit', () => {
    it('makes expected calls', () => {
        const servicingServiceStub: ServicingService = fixture.debugElement.injector.get(ServicingService);
        const storageServiceStub: StorageService = fixture.debugElement.injector.get(StorageService);
        spyOn(servicingServiceStub, 'tagSiteCatalyst');
        spyOn(servicingServiceStub, 'getServicingContent');
        spyOn(storageServiceStub, 'get');
        comp.ngOnInit();
        expect(servicingServiceStub.tagSiteCatalyst).toHaveBeenCalled();

        expect(servicingServiceStub.getServicingContent).toHaveBeenCalled();
        expect(storageServiceStub.get).toHaveBeenCalled();
    });
});

});

我在业报方面遇到了错误,Expected spy getServicingContent to have been called. 我不确定为什么会得到这个。有人可以帮我吗?

0 个答案:

没有答案