javascript - 为什么当一切都通过后 super 测试会返回错误?

标签 javascript mocha.js supertest

我的测试如下:

it('should create a user with an email address and password', function(done) {
  return request.post('/v1/users').send(defaultUser).expect(200).expect(function(res) {
    console.log(res.body);
    res.body.should.have.property('id');
    res.body.should.have.property('name');
    res.body.should.have.property('email');
    res.body.should.have.property('username');
    return res.body.should.have.property('entities')["with"].lengthOf(1);
  }).end(done);
});

res.body 看起来像

{ id: 17,
  name: 'Satoshi Nakamoto',
  email: 'snakamoto@mysite.com',
  username: 'snakamoto',
  api_key: '510e55d2c15b0617757bd463b3653ec8',
  entities: [ { id: 17, name: 'Satoshi Nakamoto Default Entity' } ] }

因此它具有所有适当的字段。我得到的错误是

  1) User Interactions should create a user with an email address and password:
 Error: [object Object]
at Test.assert (/Users/shamoon/Sites/mysite/mysite-api/node_modules/supertest/lib/test.js:218:48)
at Server.assert (/Users/shamoon/Sites/mysite/mysite-api/node_modules/supertest/lib/test.js:132:12)
at Server.g (events.js:180:16)
at Server.emit (events.js:92:17)
at net.js:1276:10
at process._tickCallback (node.js:419:13)

我做错了什么?

最佳答案

这就是问题:

return res.body.should.have.property('entities')["with"].lengthOf(1);

Supertest 处理从 expect 返回的虚假值调用需要一个函数来表示“一切都好”,并使用一个非假值来表示“有问题”。您返回的内容很可能不是假的。 should 的大多数实现返回允许标记链接的对象(例如 foo.should.something(...).and.something(...).and... )。对象是非虚假的,因此 expect认为测试失败。

关于javascript - 为什么当一切都通过后 super 测试会返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28483436/

相关文章:

node.js - NodeJS 和 super 测试 : start tests with npm command

node.js - req 和 res 对象是否可以在 Node Express 中全局访问?

javascript - jQuery 从时间集数组中挑选出时间范围

javascript - 通过单击第一个 div 设置第二个 div 的名称

javascript - Mocha : Translating a user story to a spec of describe/it (bdd framework i. Mocha )?

javascript - 在 Mocha 测试中绕过 ESLint 的 `no-unused-var` for Should

javascript - 使用 angularjs : Input textbox gives TypeError: Cannot set property '0' of undefined 在 restful API 上通过 POST 提交网络表单

javascript - 如何使用 html 表格单元格作为彩色像素显示字母?

javascript - 如何解决 'Assertions require every name in the call target to be declared with an explicit type annotation.ts(2775)' ?

javascript - Mongoose 与 Supertest 的开放连接问题