[英]REST API testing using vows, tobi and node.js
I am trying to combine the examples here , here to write a vows test for my node.js / express app that: 我试图在这里结合示例, 在这里为我的node.js / express应用编写誓言测试:
Item 1 and 2 work fine, but there is something wrong with my sub-context 'GET /users/:id'. 项目1和2可以正常工作,但是我的子上下文'GET / users /:id'出了点问题。 It errors and I cannot figure out why. 错误,我不知道为什么。 Tried Googling and using the debugger, but I still can't see what it is, I am probably just overlooking something obvious. 尝试使用Google搜索并使用调试器,但我仍然看不到它是什么,我可能只是忽略了一些明显的东西。
···✗ Errored » 3 honored ∙ 1 errored
Can anyone tell me why the 4th vow errors? 谁能告诉我为什么第四个誓言错误?
Here's my vows code: 这是我的誓言代码:
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)
I tried simplifying the code as follows to help see if this.callback was the problem, but the error is still there: 我尝试按以下方式简化代码以帮助查看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)
}
How are you populating res for the fourth tes?? 您如何填充第四个TES的资源? It wouldn't be visible outside the line 在行外不可见
'should return the new user'
Try creating the id variable outside the addBatch call, and set it in the third test. 尝试在addBatch调用之外创建id变量,然后在第三个测试中进行设置。 then call 然后打电话
client.get('/users/' + id)
EDIT : 编辑 :
Better yet, put it back into newUser in the third test: 更好的是,在第三项测试中将其放回newUser中:
'should return the new user': function(res, $){
newUser.id = res.body._id
....
and then do: 然后执行:
client.get('/users/' + newUser.id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.