使用无服务器摩卡插件测试动态端点 [英] Using serverless-mocha-plugin to test dynamic endpoint

查看:108
本文介绍了使用无服务器摩卡插件测试动态端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用无服务器框架在NodeJS中创建API应用程序.我已经安装了serverless-mocha-plugin,并试图为我的功能创建一些单元测试.

I am creating an API application in NodeJS using the Serverless framework. I have installed the serverless-mocha-plugin and am trying to create some unit tests for my functions.

在我的serverless.yml文件中,我具有以下端点:

In my serverless.yml file, I have the following endpoints:

...
equipmentGetAll:
  handler: ./api/equipment/equipment.getAll
  events:
   - http:
     path: equipment
     method: get
     cors: true
equipmentGetOne:
  handler: ./api/equipment/equipment.getOne
  events:
    - http:
      path: equipment/{po_number}
      method: get
      cors: true
...

在测试getAll端点时,我使用以下成功通过的测试.我已经通过将响应记录到控制台来验证了它的工作原理.

When testing the getAll endpoint, I use the following test which passes successfully. I have verified it works by logging the response to the console.

'use strict';

// tests for equipmentGetAll
// Generated by serverless-mocha-plugin

const mochaPlugin = require('serverless-mocha-plugin');
const expect = mochaPlugin.chai.expect;
let wrapped = mochaPlugin.getWrapper('equipmentGetAll', '/api/equipment/equipment.js', 'getAll');

describe('equipmentGetAll', () => {
  before((done) => {
    done();
  });

  it('should get all Equipment', () => {
    return wrapped.run({po_number:117}).then((response) => {
      expect(response.statusCode).to.be.equal(200);
      expect(response.body.length).to.be.greaterThan(0);
    });
  });
});

类似地,对于getOne端点,我(目前)正在做一个非常相似的测试:

Similarly, for the getOneendpoint, I am (for now) doing a very similar test:

'use strict';

// tests for equipmentGetOne
// Generated by serverless-mocha-plugin

const mochaPlugin = require('serverless-mocha-plugin');
const expect = mochaPlugin.chai.expect;
let wrapped = mochaPlugin.getWrapper('equipmentGetOne', '/api/equipment/equipment.js', 'getOne');

describe('equipmentGetOne', () => {
  before((done) => {
    done();
  });

  it('should get one single Equipment', () => {
    return wrapped.run({}).then((response) => {
      expect(response.statusCode).to.be.equal(200);
      expect(response.body.length).to.be.equal(1);
    });
  });
});

问题

我收到的对getOne的当前响应是:

The current response I'm receiving for getOne is:

{ 
  statusCode: 500,
  headers: { 'Content-Type': 'text/plain' },
  body: 'Cannot read property \'po_number\' of undefined' 
}

由于来自serverless.ymlgetOne路径equipment/{po_number},而不仅仅是equipment/.

Due to the fact that the path for getOne from serverless.yml is equipment/{po_number} rather than just equipment/.

传递测试路径值的正确方法是什么?

What is the proper way to pass the path value for the test?

一个示例调用将到达端点my-api-endpoint.com/equipment/117,并返回po_number 117的设备.在使用POSTMan进行测试时,此方法可以正常工作,但是如何使其与mocha-serverless-plugin一起工作?

A sample call would hit endpoint my-api-endpoint.com/equipment/117 and return the Equipment with po_number 117. This works properly when testing with POSTMan, but how can I make it work with mocha-serverless-plugin?

推荐答案

要将数据传递给lambda,您应该使用
wrappedLambda.run({body: "String, not Object"})

To pass data to lambda you should use
wrappedLambda.run({body: "String, not Object"})

要将queryStringParametr传递给lambda,应使用wrappedLambda.run({queryStringParameters: {a: "first",b:"second"}})

To pass queryStringParametr to lambda you should use wrappedLambda.run({queryStringParameters: {a: "first",b:"second"}})

要将pathParameters传递给lambda,应使用 wrappedLambda.run({pathParameters: {a: "first", b:"second"})

To pass pathParameters to lambda you should use wrappedLambda.run({pathParameters: {a: "first", b:"second"})

测试发布方法的简单示例

Simple example for testing post method

 context('save flashcard', () => {
        before((done) => {
            done();
        });
        it('save flashcard successfully', () => {
            return saveFlashcard.run({body: correctInput})
                .then((response) => {
                    const body = JSON.parse(response.body);
                    expect(body).to.have.property('_id')
                })
        });
});

此主体将位于事件对象内部.

this body will be located inside event object.

这篇关于使用无服务器摩卡插件测试动态端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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