单元和e2e测试grpc微服务

时间:2018-05-18 14:55:31

标签: node.js grpc nestjs

大家好我使用nestjs框架创建了一个grpc微服务。谁能给我一些关于如何测试我的grpc微服务(单位和e2e)的建议。

2 个答案:

答案 0 :(得分:0)

您可以在github中找到有关微服务e2e的官方文档,

我还在寻找的

em ..单元测试演示,

grpc-mock也许可以进行单元测试

答案 1 :(得分:-1)

这可能不是理想的选择,但是我使用Jest进行设置。希望它能帮助您入门。

beforeAll((done) => {
  client = new service.SomeService(
      'localhost:55051',
      grpc.credentials.createInsecure()
  );
  done();
});

afterAll(done => {
  client = null;
  done();
});

/**
 * Handles gRPC responses. Takes a parser to use against the response object
 * @param {function} responseParser the parser for the response object
 * @returns {function} the callback to pass in to gRPC responses
 */
const responseHandler = (onErr, responseParser, done) => (err, response) => {
  if (err) {
    return onErr(err, done)
  } else {
    return responseParser(response, done);
  }
};

describe('Authenticate', () => {
  const callback = expected => (response, done) => {
    const received = {
        status: response.status,
        challenge: bytes(response.challenge),
    };

    expect(received).toEqual(expected);
    done();

  const errHandler = expected => (err, done) => {
    expect(err).toBe('Offline')
};

  test('Gets a challenge token', (done) => {
    const expected = {
      status: testValues.SUCCESS,
      challenge: testValues.CHALLENGE
    };

    client.authenticate(
      { someKey: testValues.SOME_KEY },
      responseHandler(errHandler(expected), callback(expected), done)
    );
  });
});