30

我正在尝试使用 supertest 进行一些测试。这是我要测试的代码片段:

it("should create a new org with valid privileges and input with status 201", function(done) {
  request(app)
    .post("/orgs")
    .send({ name: "new_org", owner: "oldschool@aol.com", timezone: "America/New_York", currency: "USD"})
    .expect(201)
    .end(function(err, res) {
      res.body.should.include("new_org");
      done();
    });
});

尝试测试 res 正文时出现错误:

 TypeError: Object #<Object> has no method 'indexOf'
  at Object.Assertion.include (../api/node_modules/should/lib/should.js:508:21)
  at request.post.send.name (../api/test/orgs/routes.js:24:27)
  at Test.assert (../api/node_modules/supertest/lib/test.js:195:3)
  at Test.end (../api/node_modules/supertest/lib/test.js:124:10)
  at Test.Request.callback (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:575:3)
  at Test.<anonymous> (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10)
  at Test.EventEmitter.emit (events.js:96:17)
  at IncomingMessage.Request.end (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12)
  at IncomingMessage.EventEmitter.emit (events.js:126:20)
  at IncomingMessage._emitEnd (http.js:366:10)
  at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
  at Socket.socketOnData [as ondata] (http.js:1367:20)
  at TCP.onread (net.js:403:27)

这是超测中的错误,还是我的测试格式不正确?谢谢

4

5 回答 5

19

或者,这也应该有效:

res.body.should.have.property("name", "new_org");

另外,只是一个注释,但从逻辑上讲,我认为将其放在另一个调用expects而不是最终回调中是有意义的。这个函数也可以重复使用,所以我倾向于尽可能把它放在可重复使用的地方:

var isValidOrg = function(res) {
  res.body.should.have.property("name", "new_org");
};

it("should create a new org with valid privileges and input with status 201", function(done) {
  request(app)
    .post("/orgs")
    .send({ name: "new_org", owner: "oldschool@aol.com", timezone: "America/New_York", currency: "USD"})
    .expect(201)
    .expect(isValidOrg)
    .end(done);
});

现在你可以想象你正在测试一个GETfor/orgs/:orgId并且你可以重复使用相同的验证。

于 2014-02-07T22:41:04.523 回答
5

使用 Jest 这对我来说就像这样

  it('should do something', async () => {

    const test = await supertest(app)
      .post('/some/endpoint')
      .send({
        someResource,
      })
      .expect(200);

    expect(test.body.someKey).toBeDefined();
    expect(test.body.someKey).toBe('someValue');
  });
于 2021-05-18T16:36:58.287 回答
3

要测试响应正文,您只需将预期响应包含在expect,

const { describe, it } = require('mocha');
const supertest = require('supertest');
describe('Validate API calls', () => {
  it('create session post request should fail for invalid credentials', (done) => {
    const data = { user_name: 'incorrect_username', password: 'INVALID' };
    supertest(app).post('/api/session')
      .send(data)
      .expect('Content-Type', /json/)
      .expect({ name: 'AuthenticationError', message: 'Unauthorized' })
      .expect(401, done);
  });
});

来源:https ://willi.am/blog/2014/07/28/test-your-api-with-supertest/

于 2019-03-22T11:33:17.417 回答
0

这可以重写如下:

res.body.name.should.equal("new_org");

这将修复错误。

于 2013-01-15T14:57:14.543 回答
0

如果您的 res.body 是一个数组,则您需要提供对象的索引res.body[res.body.length -1].name.should.equal("new_org")- 如果您的属性是数组中的最后一个并且未排序

于 2017-05-25T09:54:25.273 回答