我在我的角应用程序中使用了一个控制器,我没有将“$ scope”作为此行中的参数传递:
angular.module('module').controller('MyCtrl',function() {var myCtrl = this; myCtrl.bookingId = 1}
问题是我在网上看到的所有茉莉花测试都使用“$ scope”进行测试。例如,
expect($scope.bookingId).toEqual(1);
但我不能使用范围,因为我没有在我的控制器中使用它。如何使用expect检查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);
});
});
答案 0 :(得分:0)
$controller
服务返回一个新的控制器实例。它会是
var ctrl = $controller('MyCtrl');
expect(ctrl.bookingId).toEqual(1);