我正在尝试使用Jasmine测试Meteor方法,但获得一个恒定的蓝点而不是红色/绿色以及以下错误:
(STDERR) connections property is deprecated. Use getConnections() method
(STDERR) [TypeError: Cannot call method 'split' of undefined]
我的方法位于lib/methods.js
下,我正在尝试测试的方法如下:
Meteor.methods({
copy_on_write_workout: function(day) {
if (!Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
var date = day_to_date(day, 0);
var workout = Workouts.findOne({
day: day,
owner: Meteor.userId(),
current_week: true
});
// not completed in the past. no need to copy it
if (workout.completed.length == 0 ||
(workout.completed.length == 1 &&
workout.completed.last().getTime() == date.getTime())) {
return;
}
var new_workout = workout;
if (workout.completed.last().getTime() == date.getTime()) {
new_workout.completed = [date.getTime()];
Workouts.update(workout._id, {$pop: {completed: 1}});
} else {
new_workout.completed = [];
}
Workouts.update(workout._id, {$set: {current_week: false}});
delete new_workout["_id"];
Workouts.insert(new_workout);
}
});
以下是tests/jasmine/server/unit/method.js
下的简单测试:
describe('workouts', function() {
it("copy_on_write_workout simple test", function() {
spyOn(Workouts, 'findOne');
Meteor.call('copy_on_write_workout', "Monday");
expect(Workouts.findOne).toHaveBeenCalled();
});
});
sanjo:jasmine
和velocity:html-reporter
都已安装并包含在我的软件包中。在我开始工作之前是否还需要其他设置?感谢
答案 0 :(得分:1)
Meteor方法调用将回调函数作为此处所述的最后一个参数https://forums.meteor.com/t/use-meteor-call-with-jasmine-doesnt-work/6702/2。
我通过简单地将其更改为:
来修复我的测试describe('workouts', function() {
it("copy_on_write_workout when never completed", function() {
spyOn(Workouts, 'findOne');
spyOn(Meteor, 'userId').and.returnValue(1);
Meteor.call('copy_on_write_workout', "Monday", function(err, result) {
expect(Workouts.findOne).toHaveBeenCalled();
});
});
});
此外,我添加了spyOne(Meteor, 'userId').and.returnValue(1)
,以便检查用户是否已登录。