繁体   English   中英

使用vows,tobi和node.js进行REST API测试

[英]REST API testing using vows, tobi and node.js

我试图在这里结合示例, 在这里为我的node.js / express应用编写誓言测试:

  1. 创建一个新的用户对象
  2. 检查响应是否正常
  3. 使用返回的_id测试查找新创建的用户
  4. 再次使用_id测试更新用户

项目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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM