2012-05-10 15 views
0

私はジャスミンを使ってコードをテストしています。最後の主張を除いて、すべてうまく動作します。誰かが私を助けることができますか?ジャスミン:私のアサートがコールバックで実行されないのはなぜですか?

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'; 
    }); 

    }); 

}); 

得られたログはこれです: '4fabcae0b563859269000001' の

ジャスミンノード仕様/ lockmanager.spec.jsロックが取得されています。 。 '4fabcae0b563859269000001'のロックを解除しました。 。 0.031秒2つのテスト、3つのアサーション、0失敗

これが実行されたに仕上がり

答えて

0

私は、結果が表示される前にアサートが完全に実行されたことを確認するために、最後の '実行'の後に別の 'waitsFor'が必要でした。ああ、newTestModelはafter-functionのtestModelであったはずです。

元の質問の変更されたコード。