Javascript:使用Sinon的Mocking构造函数

Javascript:使用Sinon的Mocking构造函数

问题描述:

我拉着我的头发试图找出如何使用sinon模拟构造函数。我有一个函数,将创建多个小部件通过调用接受几个参数的构造函数。我想验证构造函数用正确的参数调用正确的次数,但我不想实际构造小部件。下面的链接似乎解释了一个直接模拟构造函数的方法,但它对我来说不起作用:

I am pulling my hair out trying to figure out how to mock a constructor using sinon. I have a function that will create multiple widgets by calling a constructor that accepts a few arguments. I want to verify that the constructor is called the correct number of times with the correct parameters, but I don't want to actually construct the widgets. The following links seemingly explain a straightforward way of mocking the constructor, however it does not work for me:

使用Jasmine监听构造函数

http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine -sinon-2.html

当我对stub的构造函数进行如下调用时:

When I make the following call to stub the constructor:

sinon.stub(window, "MyWidget");

我得到以下错误:

Uncaught TypeError: Attempted to wrap undefined property MyWidget as function 

调试在Chrome我看到MyWidget显示在范围变量的本地部分,但没有MyWidget属性窗口。

When debugging in Chrome I see MyWidget shows up in the Local section of the Scope Variables, however there is not MyWidget property off of window.

任何帮助将非常感谢。 / p>

Any help would be greatly appreciated.

我能够获得 StubModule 在进行一些调整之后可以正常工作,最明显的是在stubbed模块中需要时输入async:false作为配置的一部分。

I was able to get StubModule to work after a few tweaks, most notably passing in async:false as part of the config when requiring in the stubbed module.

戴维斯先生将