samedi 24 janvier 2015

How do I stub a piece of Express middleware when making an API call with supertest?

I have a route that looks something like this:


app.post( '/api/user/register', user.register, user.doSomething, user.sendPushNotification );


I want to stub user.sendPushNotification so that my application does not actually try to send a push notification.


My user.sendPushNotification method:



exports.sendPairedPushNotification = function( req, res, next ) {
var user = req.user;

user.pushNotify( ).then( function( )
{
res.status( 200 ).send( { status: 'sucecss' } );
} );
};


My test:



var userController = require( '../../server/controllers/user' );
gently.expect( userController, 'sendPushNotification', function( req, res, next )
{
res.status( 200 ).send( { status: 'success' } );
} );

api.post( '/api/user/register' )
.send( registrationData )
.expect( 200 )
.end( function( error, res )
{
var User = mongoose.model( 'User' );

User.find( { }, function( error, users )
{
users.length.should.be.exactly( 1 );
} );
} );


But this results in an error:


Error: Expected call to [Object].sendPushNotification() did not happen


Aucun commentaire:

Enregistrer un commentaire