2

ここで例を組み合わせて、node.js / Express アプリの誓いのテストを作成しようとしています

  1. 新しいユーザー オブジェクトを作成します
  2. 応答が正しかったことを確認します
  3. 返された _id を使用して、新しく作成されたユーザーの検索をテストします
  4. 再び_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)
}
4

1 に答える 1

1

4 番目のテスの解像度をどのように設定していますか?? ラインの外では見えない

'should return the new user'

addBatch 呼び出しの外で id 変数を作成して、3 番目のテストで設定してみてください。それから電話する

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

編集

さらに良いことに、3 番目のテストでそれを newUser に戻します。

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

そして、次のようにします。

client.get('/users/' + newUser.id)
于 2011-08-02T21:32:34.460 に答える