我想测试一个发送推送通知的函数。我想将发送通知存根。我用mocha和sinon。我尝试使用几种语法来存根,但它不起作用。我试过一个存根:
var sender = sinon.createStubInstance(gcm.Sender);
var gcmReturn = {
multicast_id: 1,
success: 1,
failure: 1,
canonical_ids: 1,
results: [{
registration_id: 'xxxx',
message_id: 'yyy'
}, {
error: 'InvalidRegistration'
}]
}
sender.send = function() {
return gcmReturn;
}
要测试的代码:
var gcm = require('node-gcm');
// Function to test
NotificationService.prototype.sendSpecificNotification = function (data) {
var self = this;
// Process data to create the users, title and text parameters
return self.send(users, title, text)
.then(function(pushResults) {
// expect ...
})
};
NotificationService.prototype.send = function (users, title, text) {
var registrationIds = [];
users.forEach(function (user) {
var push = user.preferences != null ? user.preferences.push : false;
if (push) {
var regId = user.preferences.pushRegId;
registrationIds.push(regId);
}
});
var deferred = Q.defer();
if (registrationIds.length > 0) {
var message = new gcm.Message();
message.addData('message', text);
message.addData('title', title);
message.delayWhileIdle = false;
message.timeToLive = 24 * 60 * 60;
var currentDate = new Date();
var notId = currentDate.getHours() * 60 * 60 + currentDate.getMinutes() * 60 + currentDate.getSeconds();
message.addData('notId', notId);
var sender = new gcm.Sender(config.gcm.senderId);
// I want to stub that call
sender.send(message, registrationIds, function (err, result) {
if (err) {
deferred.reject(new Error(error));
} else {
deferred.resolve(result);
}
});
} else {
deferred.resolve('');
}
return deferred.promise;
}
编辑:测试
it('Subscription Push', function () {
var sender = sinon.createStubInstance(gcm.Sender);
var gcmReturn = {
multicast_id: 1,
success: 1,
failure: 1,
canonical_ids: 1,
results: [{
registration_id: 'xxxx',
message_id: 'yyy'
}, {
error: 'InvalidRegistration'
}]
}
sender.send.returns(Q.resolve(gcmReturn));
return fixtureService.clearDatabase()
.then(function () {
return fixtureService.load('./test/subscription-push.json');
})
.then(function () {
return notificationService.sendSpecificNotification();
});
});
答案 0 :(得分:0)
您是否尝试过使用sinon stub方法?例如:
it('Subscription Push', function () {
var sender = sinon.createStubInstance(gcm.Sender);
var gcmReturn = {
multicast_id: 1,
success: 1,
failure: 1,
canonical_ids: 1,
results: [{
registration_id: 'xxxx',
message_id: 'yyy'
}, {
error: 'InvalidRegistration'
}]
}
sender.send.returns(Q.resolve(gcmReturn));
return fixtureService.clearDatabase()
.then(function () {
return fixtureService.load('./test/subscription-push.json');
})
.then(function () {
return notificationService.sendSpecificNotification();
});
});
<强>更新强>
你根本没有使用存根方法,这里没有任何魔法可以在notificationService上自动存根。您需要使用您创建的发件人,或者直接在notificationService上存储该方法。
it('Subscription Push', function () {
var sender = sinon.createStubInstance(gcm.Sender);
var gcmReturn = {
multicast_id: 1,
success: 1,
failure: 1,
canonical_ids: 1,
results: [{
registration_id: 'xxxx',
message_id: 'yyy'
}, {
error: 'InvalidRegistration'
}]
}
sender.send.returns(Q.resolve(gcmReturn));
return fixtureService.clearDatabase()
.then(function () {
return fixtureService.load('./test/subscription-push.json');
})
.then(function () {
// Here you should be using sender.sendSpecificNotification();
// Alternatively, you could simply stub out the method
// on the notificationService directoy and avoid using the sender at all.
// To stub out the method on the notificationService do this:
// sinon.stub(notificationService, 'send', function() { return Q.resolve(gcmReturn); });
return notificationService.sendSpecificNotification();
});
});