首先,这是我第一次尝试在我的应用程序中运行测试。 下面的代码用于测试控制器函数的结果($ scope.getActiveClients(),此函数为返回的对象生成$ http get请求):$ scope.activeClients在我的测试运行时未定义。 到目前为止,我已经尝试了run()和waitsFor()以及jasmine的2.0文档中所述的done()函数,但无法获得此错误消息:
TypeError:undefined不是函数
describe 'myController', ->
beforeEach module('myApp')
$controller = undefined
beforeEach inject((_$controller_) ->
$controller = _$controller_
return
)
describe '$scope.activeClients', ->
it 'gets all active clients', ->
$scope = {}
controller = $controller('myController', $scope: $scope)
$scope.getActiveClients()
expect($scope.activeClients).toEqual xxx
return
return
return
任何指针都将非常感激
答案 0 :(得分:0)
您需要通过$ httpBackend模拟$ http调用,然后您可以获取您的http调用并测试异步调用。 我不知道coffescript但可以用js告诉你。
describe('myController', function(){
var $scope, controllerService, httpMock;
beforeEach(module('myApp'));
beforeEach(inject(function ($rootScope, $controller, $httpBackend) {
$scope = $rootScope.$new();
controllerService = $controller;
httpMock = $httpBackend;
}));
it("gets all active clients", function () {
//mock http calls
httpMock.expectGET("/getActiveClients").respond({data: 'clientTestData'});
ctrl = controllerService('myController', {$scope: $scope});
$scope.getActiveClients();
httpMock.flush();
expect($scope.activeClients).toEqual xxx
});
});