为什么茉莉间谍不能解决引用的函数对象? [英] Why jasmine spy doesn't resolve the function object by reference?
问题描述
我有以下简单的服务
app.factory('鞋',函数(){
函数a(){返回12;}
函数b(){返回();} 返回{
答:一,
B:B
}
})
我想测试如果方法 A,当我打电话的方法
被调用。我的测试是这样的: B
描述了('测试鞋的服务',函数(){
VAR服务; beforeEach(模块('plunker')); beforeEach(注(函数(鞋){
服务=鞋;
})) 它('。乙方应叫。一',函数(){
spyOn(服务'一');
service.b();
期待(service.a).toHaveBeenCalled();
})});
但是测试失败。相关plunker是这里。我已经知道如何解决从<一的plunker问题href=\"http://stackoverflow.com/questions/36222079/testing-angular-service-internal-method-calls\">those答案的。仍然有一个悬而未决的问题:
为什么原有的功能对象未得到解决?
我认为系统的工作原理是这样的(假设spyies装饰用额外的逻辑功能)
当我没有间谍当我打电话 service.a
它被解析为:
A)service.a - &GT;一个()
当我创建一个间谍,其装饰功能
B)间谍 - &GT; service.a =&GT; service.a *()
但 service.a
基本上是参照原 A()
,所以我们应该有一个间谍集在最终解决函数对象:
A + B =&GT;间谍 - &GT; service.a - &GT; A =&GT;一个*()
在调用 spyOn(服务'一')
,服务。一个
不再<$ C $ C> A 您在函数中定义的 - 这是一个间谍功能。这种间谍功能恰好叫 A
,但它的不是 A
的;它有一个不同的标识,并且是不同的功能。
不过,设置的
属性服务
不改变功能在
您在 app.factory
。你只是改变了服务
,以便其 A
属性不再是指原始的 A
。相比之下,你的 B
函数不改变其参照原 A
。它得到 A
直接从在本地
和 app.factory
范围 b
最初声明。该服务
替换实际上它的原始的 A
与间谍不会影响 B
的号召, A()
,因为它不引用 service.a
。
I have the following simple service
app.factory('Shoes', function() {
function a() {return 12;}
function b() {return a();}
return {
a: a,
b: b
}
})
I want to test if the method a
is being called when I call method b
. My test looks like this:
describe('Testing a Shoes service', function() {
var service;
beforeEach(module('plunker'));
beforeEach(inject(function(Shoes) {
service = Shoes;
}))
it('.b should call .a', function() {
spyOn(service, 'a');
service.b();
expect(service.a).toHaveBeenCalled();
})
});
But the tests fail. Relevant plunker is here. I've already know how to resolve the problem in the plunker from those answers. Still there is one unresolved question:
Why does the original function object is not being resolved?
I thought that the system works like this (assuming spyies decorate the function with additional logic)
When I have no spies when I call service.a
it's being resolved as:
A) service.a -> a()
and when I create a spy, it decorates the function
B) spy -> service.a => service.a*()
but the service.a
is basically a reference for original a()
so we should have a spy set for resolved function object in the end:
A + B => spy -> service.a -> a => a*()
After you call spyOn(service, 'a')
, service.a
is no longer the a
you defined in the function -- it is a spy function. That spy function happens to call a
, but it is not a
; it has a different identity and is a different function.
However, setting the a
property of service
does not change the function a
you declared inside app.factory
. You've simply changed service
so that its a
property no longer refers to your original a
. By contrast, your b
function never changes its reference to the original a
. It gets a
straight from the local app.factory
scope in which a
and b
were originally declared. The fact that service
replaces its original a
with a spy does not affect b
's call to a()
, because it does not refer to service.a
.
这篇关于为什么茉莉间谍不能解决引用的函数对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!