2012-05-10 2 views
0

일부 코드를 테스트하기 위해 재스민을 사용하고 있습니다. 마지막 주장을 제외하면 모든 것이 잘 작동합니다. 누군가 나를 도울 수 있습니까?Jasmine : 내 콜백에 내 어설 션이 실행되지 않는 이유

var mongoose = require("mongoose") 
    , db = mongoose.connect('mongodb://localhost/database') 
    , Schema = mongoose.Schema;; 


describe('Lockmanager', function() { 

    var status; 

    var Test = new Schema({ 
    name: String 
    }); 
    var testModel = mongoose.model('Test', Test); 
    var LockManager = require('locks').buildLockManager().getManager(testModel, 10000); 

    var newTestModel = new testModel({ 
    name: "Test" 
    }); 

    it('should set a lock on an arbitrary mongoose model', function() { 

    this.after(function() { 
     testModel.remove({}, function(err, numAffected) { 
     status = 'collectionRemoved'; 
     }); 
    }); 

    runs(function() { 
     newTestModel.save(function(err) { 
     expect(err).toBeNull(); 
     status = 'hasBeenSaved'; 
     }); 
    }); 
    waitsFor(function() { 
     return status == 'hasBeenSaved'; 
    }); 

    runs(function() { 
     LockManager.requestLock(newTestModel._id, function(err, response) { 
     expect(err).toBeNull(); 
     status = 'readyToBeReleased'; 
     }); 
    }); 
    waitsFor(function() { 
     return status == 'readyToBeReleased'; 
    }); 

    runs(function() { 
     LockManager.releaseLock(newTestModel._id, function(err) { 
     expect(err).toBeNull(); 
     status = 'readyToBeDeleted'; 
     }); 
    }); 

    waitsFor(function() { 
     return status == 'readyToBeDeleted'; 
    }) 

    }); 

    it('should delete all test entries after the test', function() { 

    waitsFor(function() { 
     return status == 'collectionRemoved'; 
    }); 

    runs(function() { 
     testModel.find({}, function(err, res) { 
     expect(res.length).toEqual(0); 
     status = 'allDone'; 
     }); 
    }); 

    /*** This waitsFor fixed the problem ***/ 
    waitsFor(function() { 
     return status == 'allDone'; 
    }); 

    }); 

}); 

결과 로그이있다 : 위한

자스민 노드 사양/lockmanager.spec.js 잠금 '4fabcae0b563859269000001은'획득되었다. . '4fabcae0b563859269000001'에 대한 잠금이 해제되었습니다. .

는 0.031 초 2 개 테스트 3 주장 0

고장이 실행 된

에서 마무리.

답변

0

마지막으로 '실행'후에 또 다른 'waitsFor'가 필요합니다. 결과가 표시되기 전에 어설 션이 완전히 실행되었는지 확인해야합니다. 오, 그리고 newTestModel은 after-function에서 testModel이어야합니다.

원래 질문의 수정 된 코드입니다.

관련 문제