如何在jamine中模拟路径参数-angular

时间:2018-10-04 18:14:05

标签: angular jasmine karma-jasmine

我正在尝试模拟和测试Angle中的路径参数。 这是我的组件代码

  describe('test', () => {
  let component: TestComponent;
  let fixture: ComponentFixture<TestComponent>;
  let route;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, RouterTestingModule, NgbModule.forRoot(), TestingModuleModule],
      declarations: [TestComponent],
      schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
      providers: [{ provide: ActivatedRoute, useClass: MockActivatedRoute },
        HttpTestingController,
        HttpClientTestingModule
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TestComponent);
    component = fixture.componentInstance;
    route = TestBed.get(ActivatedRoute);
    route.queryParams = of({ params: { country: 'test' , state: 'test' } });
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
   
 it('should error flag to be false', () => {
component.ngOnInit();
expect(component.hasError).toEqual(false);
  });

});

这是我的测试,我尝试调试该测试,但失败了。 我收到一个错误TypeError:运行测试时无法读取未定义的属性“ subscribe”

{{1}}

我应该采用哪种方法进行测试?

0 个答案:

没有答案