![](/img/trans.png)
[英]Meteor integration testing, rest api endpoint in velocity's mirror with jasmine
[英]Meteor client integration async testing with Velocity / Jasmine; How to get return value?
更新:注册之前和之后的用户计数仍然失败
尝试测试通过UI注册的新用户(请参见jQuery“ signUp”)。 注册之前和之后,从Method.call(“ usersCount”)进行计数的用户均返回“ 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');
}
});
};
好的,这是解决此问题的一种方法:
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/zh-CN/tutorials/es6 /承诺/
感谢@sanjo帮助我看到光明!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.