如何解决单元测试中类型错误:无法读取未定义属性(读取“returnValue”)的问题?

问题描述 投票:0回答:0

我在其中一个组件中有一个方法

loadProducts
,并编写了一个测试来检查它。结果,它返回给我一个错误:

TypeError:无法读取未定义的属性(读取“returnValue”)。

我尝试了不同的方法,但无法解决问题。帮帮我,returnValue方法在什么地方可以用,什么时候不能用?

这是我的方法:

    loadProducts(): void {
    this.category = this.activatedRoute.snapshot.paramMap.get('category') as string;
    this.productService.getAllByCategory(this.category).subscribe(data => {
      this.userProducts = data;
    })
    }

这是我对这个功能进行单元测试的整个文件:

    import { ComponentFixture, TestBed } from '@angular/core/testing';

    import { ProductsComponent } from './products.component';
    import { HttpClientTestingModule } from '@angular/common/http/testing';
    import { RouterTestingModule } from '@angular/router/testing';
    import { IProductResponse } from 'src/app/shared/interfaces/IProduct';
    import { ProductService } from 'src/app/shared/services/products/product.service';
    import { of } from 'rxjs';

    describe('ProductsComponent', () => {
    let mockProductService:any;
    let component: ProductsComponent;
    let fixture: ComponentFixture<ProductsComponent>;
    let PRODUCT: IProductResponse;
    let PRODUCTS: IProductResponse[];

    beforeEach(async () => {
    mockProductService = jasmine.createSpyObj({'getAllByCategory':of(PRODUCTS)});
    PRODUCT = {
      category: {
        id: 1,
        name: 'pizza',
        path: 'string',
        imagePath: 'string',
      },
      id: 1,
      name: 'string',
      path: 'string',
      ingredients: 'string',
      weight: 'string',
      price: 1,
      imagePath: 'string',
      count: 1,
    };
    PRODUCTS =[{
      category: {
        id: 1,
        name: 'pizza',
        path: 'string',
        imagePath: 'string',
      },
      id: 1,
      name: 'string',
      path: 'string',
      ingredients: 'string',
      weight: 'string',
      price: 1,
      imagePath: 'string',
      count: 1,
    },
    {
      category: {
        id: 1,
        name: 'pizza',
        path: 'string',
        imagePath: 'string',
      },
      id: 2,
      name: 'string',
      path: 'string',
      ingredients: 'string',
      weight: 'string',
      price: 1,
      imagePath: 'string',
      count: 1,
    }];
    await TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
        RouterTestingModule
      ],
      declarations: [ProductsComponent],
      providers:[
        {provide:ProductService, useValue:mockProductService}
      ]
    })
      .compileComponents();

    fixture = TestBed.createComponent(ProductsComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();

    });

    it('should create', () => {
    expect(component).toBeTruthy();
    });

    it('should test loadProducts()', ()=>{
    let category = 'pizza';
    mockProductService.getAllByCategory(category).and.returnValue(of(PRODUCTS))
    expect(component.userProducts).toEqual(PRODUCTS);

  });
  });
javascript angular unit-testing jasmine return-value
© www.soinside.com 2019 - 2024. All rights reserved.