mardi 9 juin 2015

how to mock $state.params in jasmine unit testing

I have the following controller EditMeetingCtrl.js

(function() {
    'use strict';

    angular
        .module('myApp')
        .controller('EditMeetingCtrl', EditMeetingCtrl);

    EditMeetingCtrl.$inject = ['$rootScope', '$scope', '$state', '$http', '$translate',
        'meetingService', 'companyService', 'notificationService', 'meeting'];

function EditMeetingCtrl($rootScope, $scope, $state, $http, $translate, meetingService, companyService, notificationService, meeting) {
        $scope.meeting = meeting;

        $scope.companyId = $state.params.companyId;
        $scope.save = save;


        function save() {
            $scope.buttonDisable = true;
            meetingService.saveMeeting($state.params.companyId, $state.params.meetingId, $scope.meeting)
                .success(function(meetingId) {
                   //more code
                });
        }


}
})();

EditMeetingCtrlSpec.js test case

describe('in EditMeetingCtrl', function () {

    var companyService , meetingService ;

    meetingId = 123321 ;                      
    companyId = 456654 ;
    meetingObj = {} ;

    var fakeHttpPromise = {
                            success: function() {}
                          };

    beforeEach(angular.mock.module('myApp'));

    beforeEach(angular.mock.inject(function (_$httpBackend_, _companyService_ , _meetingService_) {
        $httpBackend = _$httpBackend_;
        companyService = _companyService_;
        meetingService = _meetingService_ ;
    }));

    describe('EditMeetingCtrl.save()', function () {
        var $rootScope, scope, $controller , $q  ;

        beforeEach(inject(function ($rootScope, $controller , _meetingService_ ) {
            scope = $rootScope.$new();
            createController = function() {
                return $controller('EditMeetingCtrl', {
                $scope: scope,
                meeting : {} ,
                meetingService : _meetingService_
                }); 
            };
            var controller = new createController();
        }));

        it("should save the meeting object", function() {
            spyOn(meetingService, 'saveMeeting').and.returnValue(fakeHttpPromise);
            scope.save();
            expect(meetingService.saveMeeting).toHaveBeenCalledWith( meetingId , companyId , meetingObj);
        });

    });

});

When try to run the below test casse EditMeetingCtrlSpec.js i got the following test failure

PhantomJS 1.9.8 (Windows 8) EditMeetingCtrl Spying --> EditMeetingCtrl.save() should save the meeting FAILED
        Expected spy saveMeeting to have been called with [ 123321, 456654, Object({  }) ] but actual calls were [ undef
ined, undefined, Object({  }) ].

So the way understand my this problem , the service call for save() method contains $state.params.companyId, $state.params.meetingId parameters and it sends an undefined values when the service call get invoked . therefore i need to mock the the $state.params . Not sure how to do it .can anyone point me in the right direction ?

Aucun commentaire:

Enregistrer un commentaire