我正在使用 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';
});
});
});
结果日志是这样的:
jasmine-node spec/lockmanager.spec.js 已获得“4fabcae0b563859269000001”的锁定。.为“4fabcae0b563859269000001”释放了锁。.
0.031 秒内完成 2 次测试,3 次断言,0 次失败
这已被执行。