zl程序教程

您现在的位置是:首页 >  其他

当前栏目

jasmine spyOn的单步调试

调试 单步 Jasmine
2023-09-14 09:02:58 时间

如果没有调用fixture.detectChanges, element context的innerHTML里根本就看不到button的源代码:


入口:监控component的next方法调用:

 /**
     * Install a spy onto an existing object.
     * @name spyOn
     * @since 1.3.0
     * @function
     * @global
     * @param {Object} obj - The object upon which to install the {@link Spy}.
     * @param {String} methodName - The name of the method to replace with a {@link Spy}.
     * @returns {Spy}
     */
    spyOn: function(obj, methodName) {
      return env.spyOn(obj, methodName);
    },

env在jasmine.js里实现:

SpyRegistry注册表?

把Component的原始方法Component.next放置到originalMethod变量里。

然后调用createSpy创建spy:


拿到当前运行的spec:

这里能看到deliverymodeComponent的next方法已经是spied版本了:


这篇文章写得很好。

要获取更多Jerry的原创文章,请关注公众号"汪子熙":