Accessing scope.variables inside a function in jasmine

2.2k views Asked by At

I am trying to get the assert the value of the $scope.buttonDisable variable which is inside the save(). here is the test case

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

        var companyService , meetingService  ;

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

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

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

        it("buttonDisable should equal to false", function() {
            expect(controller.save.buttonDisable).toEqual(false);
        });

    });

here is my controller .

(function() {
    'use strict';

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

    EditMeetingCtrl.$inject = ['$rootScope', '$scope', '$state', '$http'];

    function EditMeetingCtrl($rootScope, $scope, $state, $http) {

        $scope.save = save;


        function save() {
            meetingService.saveMeeting()
                .success(function(meetingId) {
                    $scope.mainMeetingForm.$setPristine();
                    $scope.buttonDisable = false;

                });
        }

    }
})();

I want to test that when ever the save() get called , the scope.buttonDisable will be equal to false . from my above test i get is the following

PhantomJS 1.9.8 (Windows 8) In EditMeetingCtrl EditMeetingCtrl.save() should save the meeting FAILED
        TypeError: 'undefined' is not an object (evaluating 'controller.save.buttonDisable')

can some one enlighten me on how do i access this scope.variable inside the save() ?

1

There are 1 answers

1
Debasish Mohapatra On BEST ANSWER

See if this works,

    it("buttonDisable should equal to false", function() {
        scope.save();
        /* However here you have to flush any http request or $timeout using 
           $httpBackend.flush() and $timeout.flush() respectively 
           for success function to get called.
        */
        expect(scope.buttonDisable).toEqual(false); //assertion
    });