TypeError:无法读取Jasmine中未定义(正在读取订阅)的属性属性、中未、定义、TypeError

由网友(空.)分享简介:我正在尝试为以下方法编写测试用例:-subscriptionPackages() {this.managePackageService.getPackages().subscribe((response) => {this.packagePlan = response;},(httpErrorRes) => {});}...

我正在尝试为以下方法编写测试用例:-

 subscriptionPackages() {
    this.managePackageService.getPackages().subscribe(
      (response) => {
        this.packagePlan = response;
      },
      (httpErrorRes) => {
      }
    );
  }

我已经在我的spec.ts文件中尝试了以下代码片段来覆盖上面的代码:-

  fdescribe('AddProductComponent', () => {
  let component: AddProductComponent;
  let fixture: ComponentFixture<AddProductComponent>;
  let packageServiceStub = jasmine.createSpyObj('ManagePackageService', ['getPackages']);

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [HttpClientTestingModule,ReactiveFormsModule,FormsModule,ToastrModule.forRoot(),
        TranslateModule.forRoot(), NgSelectModule],
      declarations: [ AddProductComponent ],
      providers :[AppConfig,
        { provide: ManagePackageService, useValue: packageServiceStub }]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(AddProductComponent);
    component = fixture.componentInstance;
    component.packagePlan = [{'name':'basic', 'id':1}, {'name':'advance', 'id':2}, {'name':'premium', 'id':3}];
    fixture.detectChanges();
  });

it('should create  subscriptionPackages', () => {
  const mockpackageResponse = []; 
  packageServiceStub.getPackages.and.returnValue(of(mockpackageResponse));
  component.subscriptionPackages();
  expect(component.packagePlan).toBeTruthy();
 });
自订Configuration区段的未定义Attribute读写

从这段代码中,我覆盖了我的测试用例,但我得到以下错误:-

推荐答案

是否在组件的ngOnInitconstructor中调用subscriptionPackages

如果在ngOnInit中调用,则需要在第一个fixture.detectChanges()之前模拟returnValue。第一个fixture.detectChanges()是在调用ngOnInit时。

beforeEach(() => {
    fixture = TestBed.createComponent(AddProductComponent);
    component = fixture.componentInstance;
    // !! Move this line here if calling subscriptionPackages in ngOnInit !!
    packageServiceStub.getPackages.and.returnValue(of(mockpackageResponse));
    component.packagePlan = [{'name':'basic', 'id':1}, {'name':'advance', 'id':2}, {'name':'premium', 'id':3}];
    fixture.detectChanges();
  });

it('should create  subscriptionPackages', () => {
  const mockpackageResponse = []; 
  component.subscriptionPackages();
  expect(component.packagePlan).toBeTruthy();
 });
beforeEach(() => {
     // !! Move this line here if calling subscriptionPackages in constructor !!
    packageServiceStub.getPackages.and.returnValue(of(mockpackageResponse));
    fixture = TestBed.createComponent(AddProductComponent);
    component = fixture.componentInstance;
    component.packagePlan = [{'name':'basic', 'id':1}, {'name':'advance', 'id':2}, {'name':'premium', 'id':3}];
    fixture.detectChanges();
  });

it('should create  subscriptionPackages', () => {
  const mockpackageResponse = []; 
  component.subscriptionPackages();
  expect(component.packagePlan).toBeTruthy();
 });
阅读全文

相关推荐

最新文章