使用誓言的REST APItesting,tobi和node.js

我正在试图结合这里的例子, 在这里写我的node.js / express应用程序的誓言testing:

  1. 创build一个新的用户对象
  2. 检查响应是否理智
  3. 使用返回的_id来testing查找新创build的用户
  4. 再次使用_id来testing更新用户

第1项和第2项工作正常,但是我的子上下文“GET / users /:id”有问题。 它的错误,我不明白为什么。 试图谷歌search和使用debugging器,但我仍然不能看到它是什么,我可能只是忽略了一些明显的东西。

···✗ 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) 

编辑

我试图简化代码,以帮助看看这个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填充res? 在线外不可见

 'should return the new user' 

尝试在addBatch调用之外创buildidvariables,并将其设置在第三个testing中。 然后打电话

 client.get('/users/' + id) 

编辑

更好的是,在第三个testing中把它放回新用户:

 'should return the new user': function(res, $){ newUser.id = res.body._id .... 

然后做:

 client.get('/users/' + newUser.id)