mardi 2 juin 2015

How to invoke spyOn on a scope function

I have the following jasmine spec.

describe('ViewMeetingCtrl', function () {
    var $rootScope, scope, $controller , $q  ;

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

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

    it('the meeting type should be equal to an object', function () {
        var controller = new createController();
        //some assertion
    });

});

Following is my ViewMeetingCtrl.js

(function () {
    'use strict';
    angular.module('MyApp').controller('ViewMeetingCtrl', ViewMeetingCtrl);

    ViewMeetingCtrl.$inject = ['$scope', '$state', '$http', '$translate', 'notificationService', 'meetingService', '$modal', 'meeting', 'attachmentService'];

        function ViewMeetingCtrl($scope, $state, $http, $translate, notificationService, meetingService, $modal, meeting, attachmentService) {
            $scope.meeting = meeting;

            $scope.cancelMeeting = cancelMeeting;

            function cancelMeeting(meetingId, companyId) {
                meetingService.sendCancelNotices(companyId, meetingId)
                    .success(function () {
                        $state.go('company.view');
                    });
            }

            //more code
        }
    })();

My question that how do i invoke the spyOn (or any other jasmine spies related method) method on the above cancelMeeting() so that i can mock the method calls , returns etc. I did the following

describe('ViewMeetingCtrl', function () {
    var $rootScope, scope, $controller , $q  ;

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

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

    it('the meeting type should be equal to an object', function () {
        spyOn(scope, 'cancelMeeting');//cancelMeeting is inside the scope so did like this
        var controller = new createController();
    });

});

but i get the following output

Firefox 37.0.0 (Windows 8.1) ViewMeetingCtrl the meeting type should be equal to an object FAILED
        Error: cancelMeeting() method does not exist in C:/Users/Work/MyApp/Tests/node_mo
dules/jasmine-core/lib/jasmine-core/jasmine.js (line 1895)

is the way i am invoking spyOn is wrong or any another syntax's am i missing ?. Or do i missing something fundamental here ?

Aucun commentaire:

Enregistrer un commentaire