在 Node js 中模拟 gcp pubsub 对象。开玩笑地

Asi*_*sim 2 unit-testing node.js google-cloud-platform jestjs

我正在尝试为 GCP 云功能编写单元测试。下面是我正在尝试测试的代码片段。

//index.js
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();

exports.myFunction = functions.runWith(RUNTIME_OPTS).https.onCall(async (data, context) => {
  //pubsubbody = generated..

  //some logic

  await pubsub.topic(topicname).publish(pubsubBody, {
    platform: body.platform,
    environment: body.environment,
    event: !!event ? event : 'unknown',
  });
});
Run Code Online (Sandbox Code Playgroud)

我能够使用 firebase-functions-test 库测试 myFunction 逻辑。我做了这样的事情

//test.js
const fft = require('firebase-functions-test')();
const funcs = require('../../index');

describe('function to test viewAddRequestedProxy', () => {
  const wrapped = fft.wrap(funcs.viewAddRequestedProxy);

  // simple logic test.
  test('NullDataTest', async () => {
    const result = await wrapped(null, null);
    expect(result).toStrictEqual({output: 'xyz'});
  });
});
Run Code Online (Sandbox Code Playgroud)

现在我想测试在函数内进行 pubsub 调用的行。我尝试了很多东西,但我对 Node.js 以及一般的模拟对象确实很陌生。

我不明白如何模拟 index.js 中的 pubsub 对象并检查它是否正在发布消息。

//test.js
const fft = require('firebase-functions-test')();
const funcs = require('../../index');

describe('function to test viewAddRequestedProxy', () => {
  const wrapped = fft.wrap(funcs.viewAddRequestedProxy);

  // simple logic test.
  test('NullDataTest', async () => {
    const result = await wrapped(null, null);
    expect(result).toStrictEqual({output: 'xyz'});
  });
});
Run Code Online (Sandbox Code Playgroud)

我很困惑是否需要导出我的 pubsub 对象并访问它 test.js 。我尝试过这个,但由于某种原因"module.exports = { pubsub: pubsub };"不适合我。我还尝试使用spyOn 在 test.js 中创建一个模拟

await pubsub.topic(topicname).publish(pubsubBody, {
  platform: body.platform,
  environment: body.environment,
  event: !!event ? event : 'unknown',
});
Run Code Online (Sandbox Code Playgroud)

还尝试了手动模拟,尽管我不确定是否需要这个。通过创建一个Mocks文件夹,但似乎没有任何效果。

任何帮助将不胜感激。我想我完全偏离了轨道。

fel*_*osh 5

为了模拟节点库,您需要在其中放置一个文件__mocks__/lib-name/index.js来模拟真实的界面

在你的情况下,将一个pubsub.js文件放入__mocks__/@google-cloud/.

//pubsub.js

class PubSubMock {
  static mockInstances = [];

  static clearAllMocks() {
    PubSubMock.mockInstances.forEach((instance) =>
      Object.getOwnPropertyNames(
        instance.constructor.prototype
      ).forEach((method) => method.mockClear())
    );
    
    PubSubMock.mockInstances.length = 0;
  }

  constructor() {
    Object.getOwnPropertyNames(this.constructor.prototype).forEach((method) => {
      spyOn(this, method).and.callThrough();
    });

    PubSubMock.mockInstances.push(this);
  }

  topic(topic) {
    // you can implement here the logic
    return this;
  }

  publish(body, obj) {
    return this;
  }
}

module.exports.PubSub = PubSubMock;
Run Code Online (Sandbox Code Playgroud)

Jest 将确保每个需求都'@google-cloud/pubsub'映射到该模拟文件。

该类的模拟实现PubSub会自动监视所有类方法,这样您就可以测试是否调用了某个方法以及使用什么参数。

问题是您的生产代码创建了一个新实例,PubSub但没有公开它,因此您无法获得它的引用。一个小技巧是公开一个静态属性,该属性将保存所有创建的实例,并将由PubSub.mockInstances.

//pubsub.js

class PubSubMock {
  static mockInstances = [];

  static clearAllMocks() {
    PubSubMock.mockInstances.forEach((instance) =>
      Object.getOwnPropertyNames(
        instance.constructor.prototype
      ).forEach((method) => method.mockClear())
    );
    
    PubSubMock.mockInstances.length = 0;
  }

  constructor() {
    Object.getOwnPropertyNames(this.constructor.prototype).forEach((method) => {
      spyOn(this, method).and.callThrough();
    });

    PubSubMock.mockInstances.push(this);
  }

  topic(topic) {
    // you can implement here the logic
    return this;
  }

  publish(body, obj) {
    return this;
  }
}

module.exports.PubSub = PubSubMock;
Run Code Online (Sandbox Code Playgroud)