0

我在我的角度应用程序中使用了一个控制器,我没有将“$scope”作为这一行的参数传递:

angular.module('module').controller('MyCtrl',function() {var myCtrl = this; myCtrl.bookingId = 1}

问题是我在网上看到的所有关于 jasmine 的测试都使用“$scope”进行测试。例如,

expect($scope.bookingId).toEqual(1);

但我不能使用范围,因为我没有在我的控制器中使用它。如何使用期望检查 bookingId 的值?请在这方面帮助我。

/编辑以获得更多说明/

这是我的控制器:

angular.module('myModule').controller('MyCtrl',
['MyService', '$window', '$timeout', function(MySrv, $window, $timeout) {
    var MyCtrl= this
    MyCtrl.bookingId = 0;

这是我的测试:

describe('MyCtrlTest', function () {

beforeEach(module('myModule'));
//beforeEach(angular.mock.module('myModule'));
var $controller,  $_jspBookingUid, MyServiceMock, $scope, $ctrl;

beforeEach(inject(function(_$controller_, $rootScope, _$jspBookingUid_){
    $controller = _$controller_;
    $_jspBookingUid = _$jspBookingUid_;
    MyServiceMock = {
        getBookingId: function(){

        }

    };
    spyOn(MyServiceMock, 'getBookingId').and.returnValue(1);

    //$scope = $rootScope.$new();

    $ctrl = $controller('MyCtrl', {MyService: MyServiceMock});
}));

it('should call MyService.getBookingId() once', function() {
    expect(MyServiceMock.getBookingId).toHaveBeenCalled();
    expect(MyServiceMock.getBookingId.calls.count()).toEqual(1);
});

it('should attach bookingId to the scope(Controller)', function() {
    expect($ctrl.bookingId).toEqual(1);
});

});

4

1 回答 1

0

$controller服务返回一个新的控制器实例。它会是

var ctrl = $controller('MyCtrl');
expect(ctrl.bookingId).toEqual(1);
于 2016-01-05T09:46:55.630 回答