當談到刺探jQuery的功能(例如bind
,click
等)很容易:
spyOn($.fn, "bind");
問題是,當你想偵察$('...')
並返回定義的元素數組。
spyOn($.fn, "init").andReturn(elements); // works, but breaks stuff that uses jQuery selectors in afterEach(), etc
spyOn($.fn, "merge").andReturn(elements); // merge function doesn't seem to exist in jQuery 1.9.1
spyOn($.fn, "val").andReturn(elements); // function never gets called
那麼,如何做到這一點:
事情閱讀SO等相關答案後試過嗎?或者如果唯一的方法是窺探init
函數當我這樣做時,我如何從函數「刪除」間諜afterEach()
路由不會中斷。
jQuery版本是1.9.1。
解決方法:
我可以使它至今(醜陋的)工作的唯一方法:
realDollar = $;
try {
$ = jasmine.createSpy("dollar").andReturn(elements);
// test code and asserts go here
} finally {
$ = realDollar;
}
謝謝Eric。我使用'無法'方法,它工作正常。 – parxier