简体   繁体   English

单元测试说无法读取未定义的属性“映射”

[英]Unit test say that cannot read property 'map' of undefined

I'm trying to run a unity test on a method by mocking an addressList but it says that it cannot read the property of undefined.我正在尝试通过 mocking 和 addressList 对方法运行统一测试,但它说它无法读取未定义的属性。

The method:方法:

  async paginate(
    user: User,
    options: IPaginationOptions,
  ): Promise<Pagination<AddressResponse>> {
    const pagination = await this.addressRepository.paginate(user, options);

    return new Pagination(
      await Promise.all(
        pagination.items.map(async (address) => new AddressResponse(address)),
      ),
      pagination.meta,
      pagination.links,
    );
  }

The problem is, when I put a console.log() to read the variable "pagination" it shows me an array that is not empty on the concole:问题是,当我放置一个 console.log() 来读取变量“pagination”时,它向我显示了一个在 concole 上不为空的数组:

console.log
    addressList:  [ Address {} ]

this is what the repository is returning to me.这就是存储库返回给我的内容。

The test that I'm trying to run is this one:我要运行的测试是这个:

  describe('AddressService', () => {
  let addressService: AddressService;
  const user = new User();
  const addressList: Address[] = [new Address()];

  console.log('addressList: ', addressList);

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        AddressService,
        {
          provide: getRepositoryToken(AddressRepository),
          useValue: {
            paginate: jest.fn().mockResolvedValue(addressList),
            create: jest.fn(),
            save: jest.fn(),
            findById: jest.fn(),
            findOne: jest.fn(),
            update: jest.fn(),
            delete: jest.fn(),
          },
        },
      ],
    }).compile();

    addressService = module.get<AddressService>(AddressService);
  });

  it('should be defined', () => {
    expect(addressService).toBeDefined();
  });

  describe('paginate', () => {
    it('should return an address list successfully', async () => {
      // Act
      const result = await addressService.paginate(user, {
        page: 1,
        limit: 10,
      });

      // Assert
      expect(result).toEqual(addressList);
    });
  });
});

What is the issue with my code?我的代码有什么问题? I'm trying to fix this by days.我正试图在几天内解决这个问题。

Your paginate variable is indeed defined and populated, but the property you first accecss in the code is not.您的paginate变量确实已定义和填充,但您在代码中首先访问的属性不是。 You try to access paginate.items while your mock just returns an array of Address .您尝试访问paginate.items而您的模拟仅返回一个Address数组。 Change your paginate mock to be paginate: jest.fn().mockResolvedValue({ items: addressList }) and it should be fixed将您的paginate模拟更改为paginate: jest.fn().mockResolvedValue({ items: addressList })它应该被修复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取未定义的属性“ 0”(仅在单元测试中) - Cannot read property '0' of undefined (only on unit test) Angular 4单元测试获取错误-失败:无法读取未定义的属性“地图” - Angular 4 unit test get error - Failed: Cannot read property 'map' of undefined 反应开玩笑的单元测试用例TypeError:无法读取未定义的属性&#39;then&#39; - react jest unit test case TypeError: Cannot read property 'then' of undefined Karma Jasmine 中的 Angular 单元测试接口:无法读取未定义的属性 * - Angular Unit Test Interfaces in Karma Jasmine : Cannot Read Property * of Undefined Angular 单元测试错误 - 无法读取未定义的属性“订阅” - Angular Unit Test Error - Cannot read property 'subscribe' of undefined Angular 2 单元测试:无法读取未定义的属性“root” - Angular 2 unit test : Cannot read property 'root' of undefined 单元测试时出错 - TypeError:无法读取未定义的属性&#39;cityWeather&#39; - Error while unit test - TypeError: Cannot read property 'cityWeather' of undefined 单元测试中的Angular“无法读取未定义的属性&#39;订阅&#39;” - Angular “Cannot read property 'subscribe' of undefined” in Unit Test 单元测试主题:无法读取未定义的属性“取消订阅” - Unit test Subject: Cannot read property 'unsubscribe' of undefined TypeError:无法读取未定义的属性“ subscribe”(角度单元测试) - TypeError: Cannot read property 'subscribe' of undefined (Angular Unit-Test)
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM