大佬教程收集整理的这篇文章主要介绍了JavaScript – 间谍在Backbone.js路由电话与茉莉花,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
摘自测试
describe 'Router',-> beforeEach -> @router = new App.Router() BACkbone.history.start() afterEach -> BACkbone.history.stop() describe 'routes',-> it 'should be defined',-> expect(@router.routes).toBeDefined() describe 'default route',-> it 'should be defined',-> expect(@router.routes['']).toBeDefined() it 'should call index',-> spy = spyOn(@router,"index") @router.navigate('',truE) expect(spy).toHaveBeenCalled()
路由器
class App.Router extends BACkbone.Router routes: '' : 'index' index: -> console.log "router.index has been called"
一切都通过,除了最后一个测试“应该调用索引”.
它失败,并显示消息“预期的间谍索引已被调用”.
我试过其他变种
it "should call index",-> spyOn(@router,"index") @router.navigate('',truE) expect(@router.indeX).toHaveBeenCalled()
我也可以从原来的Router.index函数中看到“router.index已被调用”的日志输出在测试输出中
谢谢!
编辑:
一个解决方案
describe '#1 Solution',-> it 'should call index',-> spyOn(App.Router.prototype,"index") @router = new App.Router() BACkbone.history.start() @router.navigate('',truE) expect(App.Router.prototype.indeX).toHaveBeenCalled()
仍然有一些意见.
BACkbone绑定路由的方式非常难以测试.
关键是路由器方法不直接在路由器实例中调用,这些方法被称为回调,并存储在等待执行的内部BACkbone.history.route check the Backbone.Router.route code中.
此操作在路由器实例化的时刻完成,因此您必须在实例化引用之前监视您的Router.method,因此您必须在Spy已激活后延迟BACkbone.history.start.
因为您必须在创建路由器实例之前声明间谍,您必须在类级别中执行此操作.
说这是我最简单的解决方案:
describe("Router",function() { afterEach( function(){ BACkbone.history.stop(); }); it("should call index",function(){ spyOn(App.Router.prototype,"index") var router = new App.Router(); // instance created after spy activation BACkbone.history.start(); // it has to start after the Router instance is created router.navigate('',truE); expect(App.Router.prototype.indeX).toHaveBeenCalled(); }); });
结论,我认为BACkbone.Router的实现还没有一个直观的设计.
以上是大佬教程为你收集整理的JavaScript – 间谍在Backbone.js路由电话与茉莉花全部内容,希望文章能够帮你解决JavaScript – 间谍在Backbone.js路由电话与茉莉花所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。