1
我发现如果我试图模仿Angular的$ location服务,然后我需要在测试中强制任何类型的摘要,那么我会得到一个无限的摘要错误。不要紧,我的控制器中的代码是什么。我已经用一个完全空的控制器来尝试这个,它只需要$ location作为参数,如果我在我的测试中做了一个摘要,它就会失败。
Here's a fiddle,这里是从小提琴我的测试:。
describe('fooController', function() {
var controller, mocklocation, scope;
beforeEach(function() {
module("myModule");
inject(function($rootScope, $controller, $location) {
scope = $rootScope.$new();
mocklocation = sinon.stub($location);
controller = $controller("FooController", { $scope: scope, $location: location });
});
});
describe('when calling $digest in a test', function() {
beforeEach(function() {
//do some stuff here and then resolve a promise
//call digest to cause the resolved promise to get evaluated
scope.$root.$digest();
});
it('should not get infinite digests', function() {
});
});
});
请注意,我做的是一个范围$ $根消化在该测试。在我的真实代码中,我在解析之前解决了一个承诺,这就是为什么我需要做摘要。
任何建议,将不胜感激。
你能分享需要摘要的代码吗?什么样的承诺需要根摘要? –
如果需要,我可以在代码中添加一些内容,但代码本身不需要摘要。我正在解决我的茉莉花测试中的承诺,并为Angular处理promise.resolve(),我必须在测试中调用摘要。 –
如果承诺是由'$ http'生成的,那么你可以使用'$ httpBackend'。这就是为什么很高兴看到你有什么承诺要解决。 –