vendredi 1 avril 2016

Angular mocha test not firing success/error

I am currently testing a controller in mocha. The controller has an activate function which should fire success/failure based on the response. I cannot get the failure or success functions to fire during my tests.

viewController.js:

(function() {
'use strict';
angular
.module('app')
.controller('viewCtrl', viewCtrl);
function viewCtrl(Service) {
    vm.Service = Service;

    activate();
    function activate() {
        vm.Service.get().then(success, failure);
        function success(data) {
            if (!data || data == 401) {
                failure(data);
            }
        }
        function failure(error) {
            if (error) {
                console.error("Loading question failed:", error);
                vm.Service.set();
            }
        }
    }
}
})();

viewControllerTest.js:

describe('question_view_controller', function() {
    var httpBackend, controller;
    var expect = chai.expect;
    var assert = chai.assert;
    var QuestionService = {};
    var createController;

    beforeEach(function(){
        angular.mock.module('ui.router');
        angular.mock.module('question');

        Service = {
            set : sinon.stub(),

            get : sinon.stub()
        }
    })

    beforeEach(inject(function($httpBackend,$controller,$q){
        httpBackend = $httpBackend;
        createController = function(){
            return $controller('ViewCtrl', {
                $scope: scope,
                Service: Service
            });;
        }
}));

afterEach(function(){
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});

describe('activate', function () {
    describe('get.then() error', function(){
        beforeEach(function(){
            Service.get.returns(Promise.reject({error:{}}))
        })
        it('should do nothing and setFailedQuestion should be called once', function(){
            vm = createController();
            expect(vm.Service.set.callCount).to.equal('1');
        })

    })

});

});

If anyone could point out my mistake or provide any insight that would be great. Anymore questions please ask.

Aucun commentaire:

Enregistrer un commentaire