控制器
dashboardApp.controller('ExecutiveController', ['$scope', 'ExecutiveService',
function ($scope, executiveService) {
executiveService.getDashboardData().then(
function success(response) {
$scope.productsInfo = response.data;
}, function error(reason) {
console.log(reason);
});
} ]);
Controller的测试用例 '使用严格的';
describe('ExecutiveController', function () {
var scope, ctrl, mockService, $timeout;
beforeEach(module('do.dashboard'));
beforeEach(inject(function ($rootScope, $controller, $q, _$timeout_) {
mockService = jasmine.createSpyObj('ExecutiveService', ['getDashboardData']);
scope = $rootScope.$new();
$timeout = _$timeout_;
mockService.getDashboardData.andReturn($q.when(
[{Products:{Name:'Resource ',measure:'59',Work:'60'}}]
));
ctrl = $controller('ExecutiveController', {
$scope: scope,
ExecutiveService: mockService,
});
}));
it('controller should not be null', function () {
expect(ctrl).not.toBe(null);
});
it('should call the service method and set the scope data', function () {
expect(mockService.getDashboardData).toHaveBeenCalled();
$timeout.flush();
expect(scope.productsInfo.Products.Name).toEqual('Resource');
expect(scope.productsInfo.products.measure).toEqual(59);
});
});
这里当我在karma运行器中运行它时会显示错误,因为Angular Karma Test会出现“TypeError:' undefined'不是一个函数“并且它给出了it方法中存在的消息
答案 0 :(得分:0)
您的Jasmine 2.0语法错误 - 应该是
mockService.getDashboardData.and.returnValue
不
mockService.getDashboardData.andReturn