测试 gRPC 函数

Testing gRPC functions

我的任务是使用 Jest 测试 gRPC 客户端调用函数。这是典型的 node.js 函数的样子:

client.authenticate(request, meta, (error, response) => {
   if (!error) {
      console.log('REPLY FROM SERVER: ', response)
   } else {
    console.error(error)
  }
})

过程调用是回调函数,如我们所见,我无法将响应对象导出到外部变量。上面的函数是我需要测试的。我需要检查该函数是否已被正确调用。 我该怎么做呢?挣扎了一段时间了。

您可以使用 jest.spyOn(object, methodName) 模拟 client.authenticate

例如

index.ts:

import { client } from './client';

export function main() {
  const request = {};
  const meta = {};
  client.authenticate(request, meta, (error, response) => {
    if (!error) {
      console.log('REPLY FROM SERVER: ', response);
    } else {
      console.error(error);
    }
  });
}

client.ts:

export const client = {
  authenticate(request, meta, callback) {
    console.log('real implementation');
  },
};

index.test.ts:

import { main } from './';
import { client } from './client';

describe('62214949', () => {
  it('should log correct response', () => {
    const mResponse = 'mocked response';
    const logSpy = jest.spyOn(console, 'log');
    jest.spyOn(client, 'authenticate').mockImplementationOnce((request, meta, callback) => {
      console.log('mocked implementation');
      callback(null, mResponse);
    });
    main();
    expect(logSpy).toBeCalledWith('REPLY FROM SERVER: ', 'mocked response');
    expect(client.authenticate).toBeCalledWith({}, {}, expect.any(Function));
  });

  it('should handle error', () => {
    const mError = new Error('network');
    const logSpy = jest.spyOn(console, 'error');
    jest.spyOn(client, 'authenticate').mockImplementationOnce((request, meta, callback) => {
      console.log('mocked implementation');
      callback(mError);
    });
    main();
    expect(logSpy).toBeCalledWith(mError);
    expect(client.authenticate).toBeCalledWith({}, {}, expect.any(Function));
  });
});

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

 PASS  Whosebug/62214949/index.test.ts (10.557s)
  62214949
    ✓ should log correct response (23ms)
    ✓ should handle error (8ms)

  console.log
    mocked implementation

      at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)

  console.log
    REPLY FROM SERVER:  mocked response

      at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)

  console.log
    mocked implementation

      at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)

  console.error
    Error: network
        at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/Whosebug/62214949/index.test.ts:18:20)
        at Object.asyncJestTest (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)
        at resolve (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:45:12)
        at new Promise (<anonymous>)
        at mapper (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:28:19)
        at promise.then (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:75:41)
        at process._tickCallback (internal/process/next_tick.js:68:7)

       8 |       console.log('REPLY FROM SERVER: ', response);
       9 |     } else {
    > 10 |       console.error(error);
         |               ^
      11 |     }
      12 |   });
      13 | }

      at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)
      at Whosebug/62214949/index.ts:10:15
      at Object.<anonymous> (Whosebug/62214949/index.test.ts:22:7)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |      90 |      100 |   66.67 |      90 |                   
 client.ts |      50 |      100 |       0 |      50 | 3                 
 index.ts  |     100 |      100 |     100 |     100 |                   
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        12.424s

试试这个 git 模拟存储库,它提供模拟响应。你需要创建模拟 grpc 服务器并从 proto 文件添加 rpc 方法。您可以在规则中提供您的实际回复。 grpc-mock