我是Meteor和Mocha的新手。看起来我没有正确地编写测试,我想在这里提供一些帮助。
基本上这就是我想要做的事情:
。
if(Meteor.isServer) {
describe('should be able to toggle the status', function() {
// check if toggle working
it('should be able to toggle the status', function(done) {
// if app is in DB
if (Apps.findOne({ url: sampleUrl })) {
// get the current status
let status = Apps.findOne({ url: sampleUrl }).status;
// run toggle method
Meteor.call('toggle', Apps.findOne({ url: sampleUrl }), function(error) {
if (error) {
// handle error
console.log(error);
} else {
// get the current status again
const newStatus = Apps.findOne({ url: sampleUrl }).status;
// compare
expect(status).to.equal(!newStatus);
done();
}
});
}
});
});
}
问题是:
Meteor.call('toggle')
完成之前完成,如何让它等到Meteor.call完成后再进行测试? if (Apps.findOne({ url: sampleUrl }))
有时是假的,我认为当应用尚未添加到数据库时会调用此if
语句,如何确保添加应用?我将应用程序添加为:
// add app to DB before test
before(function(done) {
Meteor.call('addApp', app);
done();
});
除了// handle error
之外,console.log(error)
除了$(this).css({'background-color' : 'green'});
之外还有办法吗?我希望Mocha抛出错误并说出错误并停止继续进行。
答案 0 :(得分:1)
尝试以下代码,如果不起作用则发表评论。我们可以做到这一点。
if (Meteor.isServer) {
// For Question 2: first call to your addApp
// addApp should return true, once the app is running
const isRunning = Meteor.call('addApp', app);
assert.isTrue(isRunning);
describe('should be able to toggle the status', function() {
// check if toggle working
it('should be able to toggle the status', function(done) {
// ensure that app is defined
// so no if required
const currentApp = Apps.findOne({ url: sampleUrl });
expect(currentApp ).to.notEqual(null);
expect(currentApp ).to.notEqual(undefined);
// get the current status from app
let status = currentApp.status;
// run toggle method
Meteor.call('toggle', Apps.findOne({ url: sampleUrl }), function(error) {
if (error) {
// Regarding question 3:
done(error);
} else {
// get the current status again
const newStatus = Apps.findOne({ url: sampleUrl }).status;
// compare
expect(status).to.equal(!newStatus);
done();
}
}); // close call
}); // close it
}); // close describe
}
关于问题1我可以说,在回调中使用完成通常可以很好地使mocha等到你的Meteor.call完成。如果您按照关于问题3的描述添加完成(错误)回调,它可能会解决您的整体问题。
关于你的流星方法
通常你会模仿addApp方法的结果,例如使用sinon或手动确保,此函数中的问题不会干扰您对toggleApp的测试。我真的建议你多阅读一下嘲笑和间谍,以便有更干净,更独立的单位测试。