2012-11-10 10 views
8

테스트를 실행할 때 save() 메소드에서 오류가 계속 발생합니다. 여기 어떻게 모카와 몽구스를 사용해야합니까?

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

우리가'실제 함수에서 username' 속성을 설정하는 방법은 다음과 같습니다 당신이 달성하려고하는 무엇을 작성합니다

방법이다. 속성 ('username', 'User1'); 실제 res.send ({username : 'User1'})'또는'res.render ('home', {username : 'User1'})'로 보낼 수 있습니까? 나는이 테스트를 실행하면 –

답변

7

중첩 할 수 있습니다 설명하지만 그것은 테스트합니다. 각 테스트는 독립 실행 형이므로 사용자가 결과를보고있을 때 사용자 이름 속성을 저장했는지 여부에 따라 실패한 위치를 확인할 수 있습니다. 예를 들어 위의 코드에서 done()이 없으므로 '오류없이 저장해야합니다'테스트를 실패 할 수있는 방법이 없습니다. 위의 코드가 시간 초과되는 이유는 다음과 같습니다. mocha는 '오류없이 저장해야 함'테스트에서 done()을 찾을 수 없습니다.

중첩 할 수 있다는 것은 매우 강력합니다. 각 설명 내에서 before, beforeEach, after 및 afterEach 문을 사용할 수 있습니다. 이것들을 사용하면 위에서 시도한 중첩을 얻을 수 있습니다. 이 문장을 읽고 싶다면 mocha 문서를 참고하십시오. 우리가 user.should.have`에서 사용자 이름 속성을 가져올 수 있도록

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은 (함수 (완료) { \t \t ^^ '사용자 이름 속성을 가지고 있어야하는 것은'구문 에러 : 예기치 않은 identifier' 그것은 "그것 (" ' – chovy

+0

신경 끄시 고 좋아하지 않는다 버그를 발견했습니다. 덕분에 매우 도움이되었습니다. – chovy

+0

다른 설명을 첫 번째 설명의 두 번째 하위 항목으로 추가하면 User.rmeove()는 아무런 영향을주지 않습니다. – chovy

관련 문제