javascript Jasmine的spy投掷不是函数错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15732875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmine's spy throwing is not a function error
提问by S. Ravi Kiran
I have an Angular JS application with a module and some services in it. My controller uses these services. In Jasmine test cases, I created a mock of a service using Jasmine's createSpy
. Following is the mocked service:
我有一个 Angular JS 应用程序,其中包含一个模块和一些服务。我的控制器使用这些服务。在 Jasmine 测试用例中,我使用 Jasmine 的createSpy
. 以下是模拟的服务:
beforeEach(module(function ($provide) {
shoppingData = function () {
getAllItems: jasmine.createSpy('getAllItems');
addAnItem: jasmine.createSpy('addAnItem');
removeItem: jasmine.createSpy('removeItem');
};
$provide.value('shoppingData', shoppingData);
}));
Controller calls the getAllItems
function as soon as an object is created. I created another beforeEach
block that creates an object of the controller. Following is the test block to check if getAllItems
is called:
控制器在getAllItems
创建对象后立即调用该函数。我创建了另一个beforeEach
块来创建控制器的对象。以下是检查是否getAllItems
被调用的测试块:
it("Should call getAllItems function on creation of controller", function () {
expect(shoppingData.getAllItems).toHaveBeenCalled();
});
When I run the spec runner page on browser, the test fails with following error: TypeError: 'shoppingData.getAllItems' is not a function
当我在浏览器上运行 spec runner 页面时,测试失败并显示以下错误:TypeError: 'shoppingData.getAllItems' is not a function
I saw several similar example where this kind of test works without any issue. Can anyone point what is missing or what is going wrong here?
我看到了几个类似的例子,其中这种测试没有任何问题。谁能指出这里缺少什么或出了什么问题?
Update:I created a plunker with the part that fails
回答by Caio Cunha
Seems like a typo if this is the real code. Change the appropriated part to:
如果这是真正的代码,这似乎是一个错字。将适当的部分更改为:
shoppingData = {
getAllItems: jasmine.createSpy('getAllItems'),
addAnItem: jasmine.createSpy('addAnItem'),
removeItem: jasmine.createSpy('removeItem')
};
Just changed the function to an object and changed the ;
for ,
.
只是将函数更改;
为对象并更改了for ,
。
UPDATE 1:
更新 1:
Consider only spying at the existing object:
只考虑监视现有对象:
var deferred, _shoppingData;
beforeEach(module('shopping'));
beforeEach(inject(function(shoppingData, $q) {
_shoppingData = shoppingData;
deferred = $q.defer();
spyOn(shoppingData, 'getAllItems').andReturn(deferred.promise);
}));
it('should have called shoppingData.getAllItems', function() {
expect(_shoppingData.getAllItems).toHaveBeenCalled();
});
回答by Destron
Why don't you try:
为什么不试试:
var scope, myController;
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
myController = $controller.('controllerName', {
$scope: scope,
shoppingData: jasmine.createSpyObj('shoppingData', ['getAllItems', 'addAnItem', 'removeItem'])
});
}));