使用无服务器摩卡插件测试动态端点

时间:2019-02-21 18:21:39

标签: node.js unit-testing mocha serverless aws-serverless

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

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

...
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端点时,我使用以下成功通过的测试。我已经通过将响应记录到控制台来验证了它的工作原理。

'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端点,我(目前)正在做一个非常相似的测试:

'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的当前响应是:

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

由于来自getOne的{​​{1}}的路径serverless.yml,而不仅仅是equipment/{po_number}

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

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

2 个答案:

答案 0 :(得分:0)

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

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

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

测试发布方法的简单示例

 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')
                })
        });
});

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

答案 1 :(得分:0)

要传递身体,您需要执行以下操作

{
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    releaseDate: 2231213213,
    title: 'sfsdf',
    authorName: 'L'
  })
}