[英]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
的簡寫assert.ok
,它驗證第一個參數是否為真https://nodejs.org/api/assert.html#assert_assert_value_message
您應該改用assert.equal
或assert.strictEqual
。 另一個選擇是修改您的斷言:
assert(name === 'tomx')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.