[英]Jasmine / Karma HTTP not mocked as expected (Angular Controller on REST call)
i've been searching around for the last days but i cannot get closer to the solution. 我一直在寻找最后的日子,但我无法接近解决方案。 I try to mock the http response requested by the angular controller.
我尝试模拟角度控制器请求的http响应。
angular controller: 角度控制器:
myController = function ($http, appConfig) {
$http.post(appConfig.restPath+"/administration/imports", {
}).then(function(data){
$scope.pagination = data;
$scope.totalItems = data.data.content.length;
$scope.totalPages = data.data.totalPages;
$scope.pages = [];
$scope.imports = data.data;
for (var i = 0; i < $scope.totalPages; i++){
$scope.pages.push(i);
}
});
}
and the test: 和测试:
describe('Controller: myController', function() {
// load the controller's module
beforeEach(module("myModule"));
var controller,
scope, httpBackend, myPost;
// Initialize the controller and a mock scope
beforeEach(inject(function ($injector) {
httpBackend = $injector.get('$httpBackend');
myPost = httpBackend.whenPOST('http://localhost:9000/api/v1/administration/imports').respond({data: {content: ["a", "b"], totalPages: "1"}}, "");
scope = $injector.get('$rootScope');
var $controller = $injector.get('$controller');
createController = function() {
return $controller(myController, {'$scope' : scope });
};
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should browse the list of imported files', function() {
httpBackend.expectPOST('http://localhost:9000/api/v1/administration/imports');
var controller = createController();
httpBackend.flush();
});
});
But it seems that he wants to ask the server for the real data when i inspect the test in the chrome console (network traffic -> HTTP requests shows me, that he is requesting the server instead of loading the mocked data...), but he receives 403 (forbidden). 但是,当我在chrome控制台中检查测试时,似乎他想向服务器询问真实数据(网络流量-> HTTP请求向我显示,他是在请求服务器而不是加载模拟数据...),但他收到403(禁止)。
the error i receive by karma is the following: 我收到的因果报应的错误如下:
TypeError: Cannot read property 'length' of undefined at myController.js:35:40
line 35 is: 第35行是:
$scope.totalItems = data.data.content.length;
that makes me think that he tries to load the data from the REST service, receives 403, empty result (means data == {}
) and then he tries to access on data.data.content.length
which is undefined.... 这让我认为他尝试从REST服务加载数据,接收403,空结果(意味着
data == {}
),然后他尝试访问未定义的data.data.content.length
。...
as you can see i did it exactly like google it recommends... 如您所见,我完全像Google一样做到了,它建议...
https://docs.angularjs.org/api/ngMock/service/ $httpBackend https://docs.angularjs.org/api/ngMock/service/ $ httpBackend
Other examples on SO or anywhere else look quite similar. SO或其他任何地方的其他示例看起来都非常相似。 What am i doing wrong?
我究竟做错了什么?
yes you have to provide the real data or at least you can provide a prototype pf your data because you are testing that unit and it requires length. 是的,您必须提供真实的数据,或者至少可以提供数据的原型,因为您正在测试该单元并且需要长度。
and remove this part because you are mocking it twice 并删除此部分,因为您在嘲笑两次
httpBackend.expectPOST('http://localhost:9000/api/v1/administration/imports');
use 采用
myPost = httpBackend.expectPOST('http://localhost:9000/api/v1/administration/imports').respond({data: {content: ["a", "b"], totalPages: "1"}}, "");
this way you make sure that post is being called but if you still get same error then you have to check respond data. 这样,您可以确保正在调用该帖子,但是如果仍然遇到相同的错误,则必须检查响应数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.