在测试我的 AngularJS 控制器时,如何在 $http.get 承诺中模拟结果? [英] How do I mock the result in a $http.get promise when testing my AngularJS controller?
问题描述
经过大量阅读,从 AngularJS 控制器调用 Web 服务的推荐方法似乎是使用工厂并从中返回一个承诺.
After much reading, it seems that the recommended way to call a web service from an AngularJS controller is to use a factory and return a promise from that.
这里我有一个调用示例 API 的简单工厂.
Here I have a simple factory which calls a sample API.
myApp.factory('MyFactory', ['$http',function($http) {
var people = {
requestPeople: function(x) {
var url = 'js/test.json';
return $http.get(url);
}
};
return people;
}]);
这就是我在控制器中的调用方式
And this is how I call it in the controller
myApp.controller('MyCtrl1', ['$scope', 'MyFactory', function ($scope, MyFactory) {
MyFactory.requestPeople(22).then(function(result) {
$scope.peopleList = result;
});
}]);
虽然它工作正常,但我希望能够模拟调用 then
时传入的 result
.这可能吗?
While it works fine, I would like to be able to mock the result
that is passed in when then
is called. Is this possible?
到目前为止,我的尝试没有产生任何结果.这是我的尝试:
My attempt so far has produced nothing. This is my attempt:
//Fake service
var mockService = {
requestPeople: function () {
return {
then: function () {
return {"one":"three"};
}
}
}
};
//Some setup
beforeEach(module('myApp.controllers'));
var ctrl, scope;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('MyCtrl1', { $scope: scope, MyFactory: mockService });
}));
//Test
it('Event Types Empty should default to false', inject(function () {
expect(scope.peopleList.one).toBe('three');
}));
我在 karma runner 中运行时遇到的错误是
The error that I get when running this in karma runner, is
TypeError: 'undefined' 不是一个对象(评估 'scope.peopleList.one')
TypeError: 'undefined' is not an object (evaluating 'scope.peopleList.one')
如何让这个测试与我的模拟数据一起工作?
How can I get this test working with my mocked data?
推荐答案
我不认为 $httpBackend 是你在这里所追求的,你希望整个工厂都被嘲笑而不依赖于 $http?
I don't think $httpBackend is what you're after here, you want the whole factory to be mocked without it having a dependency on $http?
看看$q,特别是Testing下的代码示例标题.您的问题可能会通过如下所示的代码得到解决:
Take a look at $q, in particular the code sample under the Testing header. Your issue might be resolved with code that looks like this:
'use strict';
describe('mocking the factory response', function () {
beforeEach(module('myApp.controllers'));
var scope, fakeFactory, controller, q, deferred;
//Prepare the fake factory
beforeEach(function () {
fakeFactory = {
requestPeople: function () {
deferred = q.defer();
// Place the fake return object here
deferred.resolve({ "one": "three" });
return deferred.promise;
}
};
spyOn(fakeFactory, 'requestPeople').andCallThrough();
});
//Inject fake factory into controller
beforeEach(inject(function ($rootScope, $controller, $q) {
scope = $rootScope.$new();
q = $q;
controller = $controller('MyCtrl1', { $scope: scope, MyFactory: fakeFactory });
}));
it('The peopleList object is not defined yet', function () {
// Before $apply is called the promise hasn't resolved
expect(scope.peopleList).not.toBeDefined();
});
it('Applying the scope causes it to be defined', function () {
// This propagates the changes to the models
// This happens itself when you're on a web page, but not in a unit test framework
scope.$apply();
expect(scope.peopleList).toBeDefined();
});
it('Ensure that the method was invoked', function () {
scope.$apply();
expect(fakeFactory.requestPeople).toHaveBeenCalled();
});
it('Check the value returned', function () {
scope.$apply();
expect(scope.peopleList).toBe({ "one": "three" });
});
});
我已经围绕 $apply 的功能添加了一些测试,直到我开始玩这个我才知道!
I've added some tests around what $apply does, I didn't know that until I started playing with this!
哥们
这篇关于在测试我的 AngularJS 控制器时,如何在 $http.get 承诺中模拟结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!