2012-11-10 10 views
8

私はテストを実行すると、save()メソッドでエラーが発生し続けます。ここmochaとmongooseを使うべきですか?

var User = require('../../models/user') 
, should = require('should'); 

describe('User', function(){ 
    describe('#save()', function(){ 
    it('should save without error', function(done){ 
     var user = new User({ 
     username : 'User1' 
     , email  : '[email protected]' 
     , password : 'foo' 
     }); 
     user.save(function(err, user){ 
     if (err) throw err; 

     it('should have a username', function(done){ 
      user.should.have.property('username', 'User1'); 
      done(); 
     }); 
     }); 
    }) 
    }) 

}) 

はエラーです:

$ mocha test/unit/user.js 

    ․ 

    ✖ 1 of 1 test failed: 

    1) User #save() should save without error: 
    Error: timeout of 2000ms exceeded 
     at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:1 
61:14) 
     at Timer.list.ontimeout (timers.js:101:19) 
+0

我々はuser.should.have 'でユーザ名プロパティを取得するように、我々は実際の関数から' username'プロパティを設定するにはどうすればよいです。プロパティ( 'username'、 'User1'); '?実際にres.send({username: 'User1'}) 'や' res.render( 'home'、{username: 'User1'}) 'として送信できますか? –

答えて

7

ネストすることができますが説明したが、それはテストしていません。それぞれのテストは独立しているため、結果を見ているときに、どこに失敗したのかを確認することができます。たとえば上のコードでは、done()がないので、 'エラーなしで保存する'テストで失敗する方法はありません。上記のコードがタイムアウトした理由もあります:mochaは 'エラーなしで保存する'テストのdone()を見つけることができません。

しかし、ネストできることは非常に強力です。各記述内には、before、beforeEach、afterおよびafterEachステートメントがあります。これらで、あなたが上で試みている入れ子を達成することができます。これらのステートメントを読みたい場合は、mochaドキュメントを参照してください。

次のように私はあなたが達成しようとしているものを書くような方法は次のとおりです。

var User = require('../../models/user') 
    , should = require('should') 
    // this allows you to access fakeUser from each test 
    , fakeUser; 

describe('User', function(){ 
    beforeEach(function(done){ 
    fakeUser = { 
     username : 'User1' 
     , email  : '[email protected]' 
     , password : 'foo' 
    } 
    // this cleans out your database before each test 
    User.remove(done); 
    }); 

    describe('#save()', function(){ 
    var user; 
    // you can use beforeEach in each nested describe 
    beforeEach(function(done){ 
     user = new User(fakeUser); 
     done(); 
    } 

    // you are testing for errors when checking for properties 
    // no need for explicit save test 
    it('should have username property', function(done){ 
     user.save(function(err, user){ 
     // dont do this: if (err) throw err; - use a test 
     should.not.exist(err); 
     user.should.have.property('username', 'User1'); 
     done(); 
     }); 
    }); 

    // now try a negative test 
    it('should not save if username is not present', function(done){ 
     user.username = ''; 
     user.save(function(err, user){ 
     should.exist(err); 
     should.not.exist(user.username); 
     done(); 
     }); 
    }); 
    }); 
}); 
+0

私はこのテストを実行すると、私が手に: '\t \tがそれ( 'usernameプロパティを持つ必要があります'、機能(実行される){ \t \t ^^ にSyntaxError:それは用事 ' – chovy

+0

"(それを" 好きではない予期しないidentifier' – chovy

+0

最初の記述の2番目の子として別の記述を追加すると、User.rmeove()には何の効果もありません。 – chovy

関連する問題