MockFirebase replaces most of Firebase's messaging method of the admin API with simple mocks. Messaging methods will always succeed unless an error is specifically specified using failNext
.
In this example, we'll send a new message using Firebase messaging.
var mockMessaging = new MockMessaging();
var message = {
notification: {
title: 'message title',
body: 'foobar'
}
};
var result = mockMessaging.send(message);
mockMessaging.flush();
result.then(function (messageId) {
console.assert(messageId !== '', 'message id is ' + messageId);
});
- send(message: Message, dryRun?: boolean): Promise
- sendAll(messages: Array, dryRun?: boolean): Promise
- sendMulticast(message: MulticastMessage, dryRun?: boolean): Promise
In some cases it could be necessary to fake a custom send response (like a BatchResponse). In this cases you can use firebase.messaging().respondNext(methodName, result)
(similar to firebase.messaging().failNext(methodName, err)
).
var mockMessaging = new MockMessaging();
var messages = [
{
notification: {
title: 'message title',
body: 'foobar'
}
},
{
notification: {
title: 'message title',
body: 'second message'
}
}
];
var batchResponse = {
failureCount: 1,
responses: [
{
error: "something went wrong",
success: false,
},
{
messageId: "12345",
success: true,
},
],
successCount: 1,
};
mockMessaging.respondNext('sendAll', batchResponse);
var result = mockMessaging.sendAll(messages);
mockMessaging.flush();
result.then(function (response) {
console.assert(response === batchResponse, 'custom batch response is returned');
});
If you want to assert the arguments that were passed to any of the send-functions you can register a callback. The callback receives as argument that contains all arguments that were passed to the send-function.
var mockMessaging = new MockMessaging();
var message = {
notification: {
title: 'message title',
body: 'foobar'
}
};
mockMessaging.on('send', function(args) {
console.assert(args[0] === message, 'message argument is coorect');
});
mockMessaging.send(message);
mockMessaging.flush();