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
。
有什么想法吗?
答案 0 :(得分:2)
assert
只是assert.ok
的简写,它验证了第一个参数是真正的https://nodejs.org/api/assert.html#assert_assert_value_message
您应该使用assert.equal
或assert.strictEqual
代替。另一个选择是修改你的断言:
assert(name === 'tomx')