使用 Velocity / Jasmine 进行 Meteor 客户端集成异步测试;如何获得 return 值?

Meteor client integration async testing with Velocity / Jasmine; How to get return value?

已更新:注册前后的用户数仍然失败

正在尝试测试通过 UI 注册的新用户(请参阅 jQuery "signUp")。注册前后 Method.call("usersCount") 的用户数 return 'undefined'.

我在日志中看到 'undefined' -> 用户对象 -> 'undefined'。不确定为什么没有将用户数分配给规范代码中的变量。

检查 signup/logged-in 用户的第二次测试通过。

/tests/jasmine/client/integration/spec.js

// New user signup
function signUp (user, callback) {
    $('.dropdown-toggle').trigger('click');
    $('#signup-link').trigger('click');
    $('#login-username').val(user.username);
    $('#login-password').val(user.password);
    $('#login-password-again').val(user.password);
    $('#login-buttons-password').trigger('click');
    callback;
}

describe('User signup', function() {

    var user = { username: 'larry', password: 'password' };

    beforeEach(function(done) {
        Meteor.call("clearDB", done);
    });

    it('should increase users by one', function (done) {
        var userCountBefore = Meteor.call("usersCount");
        var userCountAfter = signUp(user, Meteor.call("usersCount"));
        expect(userCountBefore + 1).toEqual(userCountAfter);
    });

    it('should automatically log-in new user', function () {
        expect(Meteor.user().username).toEqual(user.username);
    });
});

/packages/test-helpers.js (自定义调试测试包;clearDB方法来自 [https://gist.github.com/qnub/97d828f11c677007cb07][1])

  if ((typeof process !== 'undefined') && process.env.IS_MIRROR) {
  Meteor.methods({
    usersCount: function () {
        var count = Meteor.users.find({}).count();
        return count;
    },
    clearDB: function(){
      console.log('Clear DB');

      var collectionsRemoved = 0;
      var db = Meteor.users.find()._mongo.db;
      db.collections(function (err, collections) {

        // Filter out velocity and system.indexes from collections
        var appCollections = _.reject(collections, function (col) {
          return col.collectionName.indexOf('velocity') === 0 ||
            col.collectionName === 'system.indexes';
        });

        // Remove each collection
        _.each(appCollections, function (appCollection) {
          appCollection.remove(function (e) {
            if (e) {
              console.error('Failed removing collection', e);
              fut.return('fail: ' + e);
            }
            collectionsRemoved++;
            console.log('Removed collection');
            if (appCollections.length === collectionsRemoved) {
              console.log('Finished resetting database');
            }
          });
        });

      });

      console.log('Finished clearing');
    }
  });
};

好的,这是解决此问题的一种方法:

it('should increase users by one', function (done) {
    Meteor.call("usersCount", function(error, userCountBefore) {
        signUp(user);
        Meteor.call("usersCount", function (error, userCountAfter) {
            expect(userCountAfter).toEqual(userCountBefore + 1);
            done();
        });
    });
});

未来的观众,请查看以下链接以了解 reference/alternate 方法: https://github.com/caolan/async https://atmospherejs.com/peerlibrary/async http://www.html5rocks.com/en/tutorials/es6/promises/

感谢@sanjo 帮我看到了曙光!