vendredi 30 janvier 2015

First async unit test with Mocha and Sinonjs

I m actually using a micro framework created by my society in which we use Mongoose.


To manage the mongoose object, we created a modelfactory, that returns us a model corresponding to the mongoose name object.


Actually, I m working on an authentication service in which I inject this modelfactory.


I need to unit test it with mocha and sinonjs, but I m a bit lost...


This is my authentication Service method that I want to test :



class AuthenticationService extends Service

constructor: (modelFactory)->
super(modelFactory)

@authorizedClientIds = [
"123456"
"toto"
]
@OAuthAccessTokensModel = @modelFactory.getSchema('OAuthAccessTokens')
@OAuthClientsModel = @modelFactory.getSchema('OAuthClients')
@OAuthUsersModel = @modelFactory.getSchema('OAuthUsers')
@OAuthRefreshTokensModel = @modelFactory.getSchema('OAuthRefreshTokens')

## Get an access token from the bearer token ##
getAccessToken: (bearerToken, callback)->
@OAuthAccessTokensModel.findOne({accessToken: bearerToken}, callback)

module.exports = AuthenticationService


I want to test the getAccessToken method, but I have clearly no idea how to make it work...


I've tried to make something like :



describe("Authentication Service", function () {

var service;

before(function () {
ModelFactory = use('/app/core/config/database/ModelFactory');

var mock = sinon.mock(ModelFactory.getFactoryInstance([]));
mock.expects("getSchema").withArgs("user").return({name:'user',getName:function(){}});

service = new AuthenticationService(mock);
});

describe("getAccessToken", function () {
it('should return-1 when the value is not present', function () {
var proxy = once(service.getAccessToken());

mock.verify();
});
});
});


How should I do to test it correctly ?


Thanks for advance


Aucun commentaire:

Enregistrer un commentaire