在 jasmine 的函数中访问 scope.variables

Accessing scope.variables inside a function in jasmine

我正在尝试获取 save() 内的 $scope.buttonDisable 变量的断言值。这是测试用例

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);
        });

    });

这是我的控制器。

(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;

                });
        }

    }
})();

我想测试当调用 save() 时,scope.buttonDisable 将等于 false。从我上面的测试中我得到以下内容

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')

有人能告诉我如何在 save() 中访问这个 scope.variable 吗?

看看这是否有效,

    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
    });