对带有承诺链的nodejs类进行单元测试 [英] Unit Test on nodejs class with chain of promise

查看:97
本文介绍了对带有承诺链的nodejs类进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能帮我为Publish方法编写单元测试吗?它应该覆盖左侧提到的那一行.

Can you please help me to write unit test for Publish method. It should cover the line mentioned left side.

导入语句"amqplib"需要被嘲笑.

The import statement 'amqplib' need to be mocked in jest.

ch.publish和conn.close等

ch.publish and conn.close, etc should be asserted

import { Injectable, Inject } from '@nestjs/common';
import * as amqp from 'amqplib';
import { RabbitMQOptions } from '../interface';
import { RABBITMQ_OPTIONS } from '../constant';

@Injectable()
export class EmitDirectService {
  constructor(
    @Inject(RABBITMQ_OPTIONS)
    private readonly rabbitMQOptions: RabbitMQOptions[],
  ) {}

  publish(queueName: string, message: string) {
    let rabbitMQOption = this.rabbitMQOptions.filter(x => x.name == queueName);
    if (!rabbitMQOption) {
      throw Error('Connection string does not exist');
    }
    amqp
      .connect(rabbitMQOption[0].connection)
      .then(function(conn) {
        return conn
          .createChannel()
          .then(function(ch) {
            var ex = rabbitMQOption[0].exchangeName;
            var ok = ch.assertExchange(ex, rabbitMQOption[0].exchangeType, {                        // Need Test Coverage
              durable: false,
            });

            return ok.then(function() {                                                             //Need test coverage all subsequent 2 line
              ch.publish(ex, rabbitMQOption[0].key, Buffer.from(message));

              return ch.close();
            });
          })
          .finally(function() {
            conn.close();                                                                           // Need test Coverage
          });
      })
      .catch(console.warn);
  }
}

推荐答案

听说过Google吗?

Ever heard of Google?

受Nock启发的AMQP模拟和期望库

这篇关于对带有承诺链的nodejs类进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆