开玩笑地期望一个带有内部功能的对象

时间:2020-04-17 09:52:07

标签: javascript node.js unit-testing jestjs jest-fetch-mock

我正在编写一个Jest测试,其中调用了一个函数,并期望返回一个对象,如下所示:

const repository = container => {

  const makeBooking = (user, booking) => {
    'make booking function called'
  }

  const generateTicket = (paid, booking) => {
    console.log('generate ticket function called')
  }

  const getOrderById = orderId => {
    console.log('get order by ID called')
  }

  const disconnect = () => {
    console.log('disconnect method called')
  }

  return {
    makeBooking,
    getOrderById,
    generateTicket,
    disconnect
  }
}

为示例起见,所有这些功能现在都是示例。我现在导出函数,然后在测试中使用它,如下所示:

container = {}

describe('Repository', () => {
  it('should connect with a container', () => {
    let hello = repository(container)

    expect(hello).toMatchObject({
      makeBooking: jest.fn('makeBooking'),
      getOrderById: jest.fn('getOrderById'),
      generateTicket: jest.fn('generateTicket'),
      disconnect: jest.fn('disconnect')
    })
  })
})


我遇到的错误是:

 Expected value to match object:
      {"disconnect": [Function mockConstructor], "generateTicket": [Function mockConstructor], "getOrderById": [Function mockConstructor], "makeBooking": [Function mockConstructor]}
    Received:
      {"disconnect": [Function disconnect], "generateTicket": [Function generateTicket], "getOrderById": [Function getOrderById], "makeBooking": [Function makeBooking]}

我需要模拟从存储库对象返回的对象。但是,该对象由命名函数组成,如代码中所示。 反正有没有模拟其中具有功能的对象?

2 个答案:

答案 0 :(得分:1)

您可以使用jest.spyOnrepository的方法创建存根。

模拟或存根存储库的方法,需要使用它们。这就是为什么service.js来自的原因,您当然可以在任何地方使用存储库。因此,实际上要测试的方法是service.makeBooking。然后,您可以声明存储库的makeBooking方法,例如,检查是否调用了存储库的模拟/存根makeBooking方法。

在这里,我们使用依赖项注入模式将模拟的hello对象注入到service.makeBooking(hello)方法中。

例如

repository.js

const repository = (container) => {
  const makeBooking = (user, booking) => {
    'make booking function called';
  };

  const generateTicket = (paid, booking) => {
    console.log('generate ticket function called');
  };

  const getOrderById = (orderId) => {
    console.log('get order by ID called');
  };

  const disconnect = () => {
    console.log('disconnect method called');
  };

  return {
    makeBooking,
    getOrderById,
    generateTicket,
    disconnect,
  };
};

module.exports = repository;

repository.test.js

const repository = require('./repository');

const container = {};

describe('Repository', () => {
  it('should connect with a container', () => {
    let hello = repository(container);

    expect(hello).toMatchObject({
      makeBooking: expect.any(Function),
      getOrderById: expect.any(Function),
      generateTicket: expect.any(Function),
      disconnect: expect.any(Function),
    });
  });

  it('should generate ticket', () => {
    let hello = repository(container);
    const logSpy = jest.spyOn(console, 'log');
    hello.generateTicket();
    expect(logSpy).toBeCalledWith('generate ticket function called');
  });

  // rest test cases same as above
});

具有覆盖率报告的单元测试结果:

 PASS  stackoverflow/61268658/repository.test.js (11.281s)
  Repository
    ✓ should connect with a container (5ms)
    ✓ should generate ticket (19ms)

  console.log node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866
    generate ticket function called

---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |      80 |      100 |      40 |      80 |                   
 repository.js |      80 |      100 |      40 |      80 | 11,15             
---------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        13.132s

service.js

const service = {
  makeBooking(hello) {
    return hello.makeBooking();
  },
};

module.exports = service;

service.test.js

const service = require('./service');
const repository = require('./repository');
const container = {};

describe('service', () => {
  it('should init', () => {
    let hello = repository(container);
    jest.spyOn(hello, 'makeBooking').mockReturnValueOnce('fake data');
    const actual = service.makeBooking(hello);
    expect(actual).toEqual('fake data');
    expect(hello.makeBooking).toBeCalledTimes(1);
  });
});

具有覆盖率报告的单元测试结果:

 PASS  stackoverflow/61268658/service.test.js (10.94s)
  service
    ✓ should init (4ms)

---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |   76.92 |      100 |   33.33 |   76.92 |                   
 repository.js |      70 |      100 |      20 |      70 | 7,11,15           
 service.js    |     100 |      100 |     100 |     100 |                   
---------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        12.278s

答案 1 :(得分:0)

这就是我要找的:

expect.objectContaining({ 
   theProperty: expect.any(Function) 
})