更新时间:2023-12-04 17:27:23
调用 spyOn(service,'a')
后, service.a
不再是您在函数中定义的 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.
但是,设置 service
的 a
属性不会更改您在 app.factory
中声明的 function a
>.您只需更改 service
,以使其 a
属性不再引用原始的 a
.相比之下,您的 b
函数永远不会将其引用更改为原始的 a
.它直接从最初声明了 a
和 b
的本地 app.factory
范围获取 a
. service
用间谍替换其原始 a
的事实不会影响 b
对 a()
的调用,因为它没有引用 service.a
.
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
.