lundi 29 février 2016

How to test service with a resource inside of it

So I have a fairly simple service function that takes a meeting resource, and decides if the user has permission to write to it based in the return status from the server. This works fine, but I am struggling with how to write the unit tests for it.

The service:

rmtg.service('share_manager', ['$log', function($log) {

...

    this.getWritablePermission = function (meeting, callback) {

        var originalTitle = meeting.Description;

        meeting.Description = 'getWritablePermission Test'

        meeting.$put().then(function (serverData) {
            meeting.Description = originalTitle;
            meeting.$put().then(function (serverData) {
                // sets isWriteable in scope to true
                callback(true);
            });
        }).catch(function (serverData) {
            callback(false);
        });
    };
 }]);

and the test so far:

describe("share_manager service", function(){
     beforeEach(module("rmtg"));

    var share_manager;
    beforeEach(inject(function(_share_manager_, _$httpBackend_){
        share_manager = _share_manager_;
        $httpBackend = _$httpBackend_;
    }));

    ...

    describe(".getWritablePermission", function () {
        var mockMeeting = {
            Description: 'Mock Title',
            $put: function(){
               return
            }
        };
        var mockCallback = function(){};
    });
});

Here is where I got stuck. How am I supposed to mock a promise like this? The Meeting is a resource that is defined somewhere else, and want to mock it for the purpose of the service test, so long as it returns a promise that I can then fail/succeed.

I looked at the $httpBackend service, but that doesn't deal with promises, and the Jasmine SpyOn documentation didn't seem to deal with it either.

Aucun commentaire:

Enregistrer un commentaire