ここで例を組み合わせて、node.js / Express アプリの誓いのテストを作成しようとしています。
- 新しいユーザー オブジェクトを作成します
- 応答が正しかったことを確認します
- 返された _id を使用して、新しく作成されたユーザーの検索をテストします
- 再び_idを使用して、ユーザーの更新をテストします
項目 1 と 2 は問題なく動作しますが、サブコンテキスト 'GET /users/:id' に何か問題があります。エラーが発生し、理由がわかりません。グーグルでデバッガーを使用してみましたが、それが何であるかはまだわかりません。おそらく、明らかなことを見落としているだけです。
···✗ Errored » 3 honored ∙ 1 errored
4番目の誓いが間違っている理由を誰か教えてもらえますか?
これが私の誓いのコードです:
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)
}