我正在使用Jasmine来测试是否创建了某些对象并调用了它们的方法.
我有一个jQuery小部件,它创建flipcounter对象并调用它们的setValue方法.flipcounter的代码在这里:https://bitbucket.org/cnanney/apple-style-flip-counter/src/13fd00129a41/js/flipcounter.js
flipcounters使用以下方式创建:
var myFlipCounter = new flipCounter("counter", {inc: 23, pace: 500});
Run Code Online (Sandbox Code Playgroud)
我想测试创建flipcounters并调用setValue方法.我的问题是如何在创建这些对象之前监视这些对象?我是否会监视构造函数并返回虚假对象?示例代码确实会有所帮助.谢谢你的帮助!:)
更新:
我已经尝试过像这样监视flipCounter了:
myStub = jasmine.createSpy('myStub');
spyOn(window, 'flipCounter').andReturn(myStub);
//expectation
expect(window.flipCounter).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)
然后通过flipCounter测试setValue调用:
spyOn(myStub, 'setValue');
//expectation
expect(myStub.setValue).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)
初始化flipCounter的第一个测试很好,但是为了测试setValue调用,我得到的是'setValue()方法不存在'错误.我这样做是对的吗?谢谢!