【发布时间】:2013-01-12 05:34:13
【问题描述】:
我正在努力弄清楚如何使用 sinon 模拟构造函数。我有一个函数,它将通过调用一个接受一些参数的构造函数来创建多个小部件。我想验证构造函数是否使用正确的参数被调用了正确的次数,但我不想实际构造小部件。以下链接似乎解释了模拟构造函数的简单方法,但它对我不起作用:
Spying on a constructor using Jasmine
http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html
当我对构造函数进行以下调用时:
sinon.stub(window, "MyWidget");
我收到以下错误:
Uncaught TypeError: Attempted to wrap undefined property MyWidget as function
在 Chrome 中调试时,我看到 MyWidget 显示在 Scope 变量的 Local 部分中,但是窗口中没有 MyWidget 属性。
任何帮助将不胜感激。
【问题讨论】:
-
请注意,这篇文章没有为所述问题提供任何实际解决方案。
标签: javascript unit-testing constructor mocking sinon