更新:注册前后的用户数仍然失败
尝试测试通过UI注册的新用户(请参阅jQuery“signUp”)。注册前后,Method.call(“usersCount”)的用户数均返回'undefined'。
我看到'undefined' - >用户对象 - >日志中的'undefined'。不确定为什么没有将用户数分配给规范代码中的变量。
检查注册/登录用户的第二次测试通过。
/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 (自定义调试测试包;来自 [https://gist.github.com/qnub/97d828f11c677007cb07][1]的clearDB方法)
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');
}
});
};
答案 0 :(得分:1)
好的,这是解决这个问题的一种方法:
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();
});
});
});
未来的观众,请查看以下链接以获取参考/替代方法: https://github.com/caolan/async https://atmospherejs.com/peerlibrary/async http://www.html5rocks.com/en/tutorials/es6/promises/
感谢@sanjo帮我看光!