How do I spy on methods?
const spy = spyOn(obj, 'methodName');
Replaces obj.methodName and returns a handle:
.calls— array of argument arrays, one per call.callCount—calls.length.lastCall— args of the most recent call, orundefined.andCallFake(fn)— delegate subsequent calls tofn.andReturn(value)— short-circuit subsequent calls to returnvalue.restore()— puts the original method back
With neither .andCallFake nor .andReturn set, calls pass through to
the original implementation while still being recorded — spies default to
transparent, not silent.
Core helpers (DomHelper, SecurityHelper, NavigationManager, …) are
modules of functions, imported as read-only namespaces: spyOn can't replace
their functions. Spy on what the function calls instead (e.g.
spyOn(window.history, 'replaceState')), or check its effect.