我正在使用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 Lock for ' 4fabcae0b563859269000001'已被收购。 。释放锁定 ' 4fabcae0b563859269000001&#39 ;.
在0.031秒内完成2次测试,3次断言,0次失败
这已经执行了。
答案 0 :(得分:0)
原来我在最后一次'运行'之后需要另一个'waitsFor'来确保断言在结果显示之前完全执行。哦,newTestModel应该是后续函数中的testModel。
原始问题中的修改代码。