【问题标题】:Using serverless-mocha-plugin to test dynamic endpoint使用 serverless-mocha-plugin 测试动态端点
【发布时间】:2019-02-21 18:21:39
【问题描述】:

我正在使用无服务器框架在 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);
    });
  });
});

同样,对于getOneendpoint,我(目前)正在做一个非常相似的测试:

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

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

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

示例调用会到达端点my-api-endpoint.com/equipment/117 并返回带有po_number 117 的设备。这在使用 POSTMan 进行测试时可以正常工作,但我怎样才能使它与 mocha-serverless-plugin 一起工作?

【问题讨论】:

  • 有什么更新吗?遇到同样的问题

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


【解决方案1】:

要将数据传递给 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"})

测试post方法的简单示例

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

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

【讨论】:

    【解决方案2】:

    要传递身体,你需要做这样的事情

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

    【讨论】:

      猜你喜欢
      • 2018-11-06
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多