我有一个UserService,它将myService作为依赖项。麻烦的是,在UserService中,myService.getAll被调用。然而,在我的业力茉莉花测试中,它抱怨myService.getAll未定义。这是因为在$ provide创建UserService之前,我还没有将getAll方法添加到mockMyService。我之前会创建getAll方法,但getAll有$ q作为依赖项,我无法及时获得$ q。在我使用$ provide之前,我不能使用注射器。在UserService调用mockMyService.getAll之前,如何使用$ q将getAll方法附加到mockMyService?
beforeEach(function() {
mockMyService = {};
module('plunker');
module(function ($provide) {
$provide.value('myService', mockMyService);
});
/* at this point - getAll is called */
inject(function ($rootScope, _$q_) {
rootScope = $rootScope
q = _$q_;
});
mockMyService.getAll = function () {
var deferred = q.defer();
deferred.resolve([
{ itemText: "Foo" },
{ itemText: "Bar" }
]);
return deferred.promise;
};
}