[英]REST API testing using vows, tobi and node.js
我試圖在這里結合示例, 在這里為我的node.js / express應用編寫誓言測試:
項目1和2可以正常工作,但是我的子上下文'GET / users /:id'出了點問題。 錯誤,我不知道為什么。 嘗試使用Google搜索並使用調試器,但我仍然看不到它是什么,我可能只是忽略了一些明顯的東西。
···✗ Errored » 3 honored ∙ 1 errored
誰能告訴我為什么第四個誓言錯誤?
這是我的誓言代碼:
var vows = require('vows')
, assert = require('assert')
, tobi = require('tobi')
var suite = vows.describe('Users API')
, now = new Date().getTime()
, newUser = { name: now + '_test_user', email: now + '@test.com' }
, browser = tobi.createBrowser(3000, 'localhost')
, defaultHeaders = { 'Content-Type': 'application/json' }
function assertStatus(code) {
return function (res, $) {
res.should.have.status(code)
}
}
var client = {
get: function(path) {
return function() {
browser.get(path, { headers: defaultHeaders }, this.callback)
}
},
post: function(path, data) {
return function() {
browser.post(path, { body: JSON.stringify(data), headers: defaultHeaders }, this.callback)
}
}
}
suite.addBatch({
'GET /users': {
topic: client.get('/users'),
'should respond with a 200 ok': assertStatus(200)
},
'POST /users': {
topic: client.post('/users', newUser),
'should respond with a 200 ok': assertStatus(200),
'should return the new user': function(res, $){
assert.isNotNull(res.body._id)
assert.isNotNull(res.body.created_at)
assert.isTrue(res.body._id.length > 0)
assert.equal(newUser.name, res.body.name)
assert.equal(newUser.email, res.body.email)
},
'GET /users/:id': { // Sub-context of POST /users
topic: function(res) { return client.get('/users/' + res.body._id) },
'should respond with a 200 ok': assertStatus(200)
}
}
})
suite.export(module)
我嘗試按以下方式簡化代碼以幫助查看this.callback是否是問題所在,但錯誤仍然存在:
'GET /users/:id': { // Sub-context of POST /users
topic: function(res) {
console.log('About to request /users/' + res.body._id)
browser.get('/users/' + res.body._id, { headers: defaultHeaders }, this.callback)
},
'should respond with a 200 ok': assertStatus(200)
}
您如何填充第四個TES的資源? 在行外不可見
'should return the new user'
嘗試在addBatch調用之外創建id變量,然后在第三個測試中進行設置。 然后打電話
client.get('/users/' + id)
編輯 :
更好的是,在第三項測試中將其放回newUser中:
'should return the new user': function(res, $){
newUser.id = res.body._id
....
然后執行:
client.get('/users/' + newUser.id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.