【问题标题】:Jasmine: Why is my assert not executed in my callbackJasmine:为什么我的断言没有在我的回调中执行
【发布时间】:2012-05-19 03:14:54
【问题描述】:

我正在使用 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 次失败

这已经被执行了。

【问题讨论】:

    标签: javascript node.js asynchronous mongoose jasmine


    【解决方案1】:

    事实证明,在最后一次“运行”之后,我需要另一个“waitsFor”,以确保在显示结果之前完全执行了断言。哦对了,newTestModel 应该是 after-function 中的 testModel。

    原始问题中的修改代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-05
      • 2017-07-31
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 2021-06-18
      相关资源
      最近更新 更多