使用 Mocha 和 Supertest 进行 NodeJS 单元测试 - `assert` 似乎没有按预期工作?
NodeJS unit testing with Mocha and Supertest - `assert` does not seem working as expected?
assert
似乎没有按预期工作。
这是我的 package.json:
"devDependencies": {
"babel-eslint": "^7.2.3",
"backpack-core": "^0.4.1",
"mocha": "^3.5.0",
"supertest": "^3.0.0"
},
"scripts": {
"test": "NODE_PATH=./server:./server/modules mocha ./test/*.js --compilers js:babel-core/register --recursive",
"dev": "backpack dev",
"build": "nuxt build && backpack build",
"start": "cross-env NODE_ENV=production node build/main.js"
},
我的测试:
'use strict'
import app from '../server/index'
import supertest from 'supertest'
import assert from 'assert'
const request = supertest.agent(app)
var _id
var name
describe('POST /api/users with data', function () {
it('status code should be 200', function (done) {
request
.post('/api/users')
.type('form')
.send({name: 'tom'})
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
console.log(res.body)
if (err) return done(err)
name = res.body.ops[0].name.toString()
_id = res.body.ops[0]._id.toString()
console.log(name)
assert(name, 'tomx') // should not pass this.
done()
})
})
})
结果:
POST /api/users with data
{ result: { ok: 1, n: 1 },
ops: [ { id: 'xxx', name: 'tom', _id: '59a7c33ba17bd32431c4134b' } ],
insertedCount: 1,
insertedIds: [ '59a7c33ba17bd32431c4134b' ] }
tom
✓ status code should be 200 (54ms)
在assert(name, 'tomx')
- 为什么通过了?应该通过的是tom
有什么想法吗?
assert
只是 assert.ok
的 shorthand,它验证第一个参数是真实的 https://nodejs.org/api/assert.html#assert_assert_value_message
您应该改用 assert.equal
或 assert.strictEqual
。另一种选择是修改您的断言:
assert(name === 'tomx')
assert
似乎没有按预期工作。
这是我的 package.json:
"devDependencies": {
"babel-eslint": "^7.2.3",
"backpack-core": "^0.4.1",
"mocha": "^3.5.0",
"supertest": "^3.0.0"
},
"scripts": {
"test": "NODE_PATH=./server:./server/modules mocha ./test/*.js --compilers js:babel-core/register --recursive",
"dev": "backpack dev",
"build": "nuxt build && backpack build",
"start": "cross-env NODE_ENV=production node build/main.js"
},
我的测试:
'use strict'
import app from '../server/index'
import supertest from 'supertest'
import assert from 'assert'
const request = supertest.agent(app)
var _id
var name
describe('POST /api/users with data', function () {
it('status code should be 200', function (done) {
request
.post('/api/users')
.type('form')
.send({name: 'tom'})
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
console.log(res.body)
if (err) return done(err)
name = res.body.ops[0].name.toString()
_id = res.body.ops[0]._id.toString()
console.log(name)
assert(name, 'tomx') // should not pass this.
done()
})
})
})
结果:
POST /api/users with data
{ result: { ok: 1, n: 1 },
ops: [ { id: 'xxx', name: 'tom', _id: '59a7c33ba17bd32431c4134b' } ],
insertedCount: 1,
insertedIds: [ '59a7c33ba17bd32431c4134b' ] }
tom
✓ status code should be 200 (54ms)
在assert(name, 'tomx')
- 为什么通过了?应该通过的是tom
有什么想法吗?
assert
只是 assert.ok
的 shorthand,它验证第一个参数是真实的 https://nodejs.org/api/assert.html#assert_assert_value_message
您应该改用 assert.equal
或 assert.strictEqual
。另一种选择是修改您的断言:
assert(name === 'tomx')