繁体   English   中英

使用Jasmine监视Backbone.js路由调用

[英]Spying on Backbone.js route calls with Jasmine

在Backbone Router上监视方法调用时遇到问题,以确保它在给定路由上调用正确的方法。

从测试中摘录

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()

我花了太多时间来找一个工作的jsFiddle ,这个问题已经被@MarkRushakoff回答了。

我还有一些评论。

Backbone绑定路由的方式非常难以测试。

关键是路由器方法不直接在路由器实例中调用,方法被作为回调并存储在内部Backbone.history.route等待执行, 检查Backbone.Router.route代码

此操作在Router实例化时完成,因此您必须在实例化引用之前spy您的Router.method ,因此您必须在激活spy后延迟Backbone.history.start

由于您必须在创建路由器实例之前声明spy ,因此您必须在级别中执行此操作。

这么说这是我带来的最简单的解决方案:

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实现没有直观的设计。

我很确定这与使用路由哈希时Backbone绑定到路由方法的方式有关(特别是如果你看到控制台日志正确输出)。 也就是说,路由器绑定了原始index方法,但是你的间谍已经取代了“当前” index方法。

您有两种选择:

  • spyOn(@router, "index")在路由器绑定到路由之前(可能很难)
  • spyOn(App.router.prototype, "index"); @router.navigate('', true); expect(App.router.prototype.index).toHaveBeenCalled();原型的index方法: spyOn(App.router.prototype, "index"); @router.navigate('', true); expect(App.router.prototype.index).toHaveBeenCalled(); spyOn(App.router.prototype, "index"); @router.navigate('', true); expect(App.router.prototype.index).toHaveBeenCalled();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM