我是单元测试的新手,并且在angularjs框架中使用了茉莉和业力。 我正在尝试为具有以下功能的控制器编写一个单元测试:
function init(){
vm.itemList = {};
ajax.get(address.base(['users'])).then(
function (response) {
vm.itemList = response.data.data;
uiGridAssistant.generateColumns(columns, vm, vm.itemList, {contextmenuOptions: 'ctrl.rightClick'}).then(
function (response) {
vm.gridOptions = response;
},
function () { }
)
},
function () {
vm.noData = true;
}
);
getBaseInfo();
}
我想测试在对服务器的ajax调用内使用uiGridAssistant服务的init函数,但找不到任何解决方案。 这是我的测试要求。
describe('users Controller', function() {
var scope, controller, deferred, $httpBackend;
beforeEach(angular.mock.module('ui.router'));
beforeEach(angular.mock.module('app'));
beforeEach(inject(function($injector, $controller, _$q_, _uiGridAssistant_) {
var $rootScope = $injector.get('$rootScope');
deferred = _$q_.defer();
scope = $rootScope.$new();
spyOn(_uiGridAssistant_, 'generateColumns').and.returnValue(deferred.promise);
$httpBackend = $injector.get('$httpBackend');
controller = $controller('users', {'$scope': scope, uiGridAssistant: _uiGridAssistant_});
}));
it('should returns grid options as expected', function () {
controller.functions.init();
$httpBackend.expectGET('http://users').respond(200);
deferred.resolve(testGridOptionsColumns);
scope.$apply();
expect(controller.gridOptions).toBeDefined();
});
});
非常感谢!