刺探Backbone.js的路线茉莉调用 [英] Spying on Backbone.js route calls with Jasmine

查看:195
本文介绍了刺探Backbone.js的路线茉莉调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有问题刺探方法中的骨干路由器呼吁,以确保它卡列斯某一特定航线上正确的方法。

从测试摘录

 描述路由器, - >
    beforeEach - >
        @router =新App.Router()
        Backbone.history.start()    afterEach - >
        Backbone.history.stop()    描述'路线', - >
         它应该被定义', - >
              期待(@ router.routes).toBeDefined()         描述默认路由, - >
             它应该被定义', - >
                  期待(@ router.routes [''])。toBeDefined()             它应该调用索引, - >
                 间谍= spyOn(@router,指数)
                 @ router.navigate('',真)
                 期待(间谍).toHaveBeenCalled()

路由器

 类App.Router扩展Backbone.Router
    路线:
        '':'指数'    索引: - >
        的console.logrouter.index被称为

经过的一切,除了最后测试应该叫指数。
它失败的消息预期间谍指数被称为。
香港专业教育学院尝试过其他的变种

 它应该叫指数 - >
    spyOn(@router,指数)
    @ router.navigate('',真)
    期待(@ router.index).toHaveBeenCalled()

我也可以看到router.index被称为从原来的Router.index功能

输出测试日志输出

谢谢!

编辑:
一种解决方案

 描述'#​​1解决方案, - >
    它应该调用索引, - >
        spyOn(App.Router.prototype,指数)
        @router =新App.Router()
        Backbone.history.start()
        @ router.navigate('',真)
        期待(App.Router.prototype.index).toHaveBeenCalled()


解决方案

它已经花了太多的时间给我配备了工作的jsfiddle 和问题已通过@MarkRushakoff被已经回答了。

不过,我有一些意见。

的方式骨干结合的路线使得很难对其进行测试。

的要点是,在路由器的方法的不直接在路由器称为<青霉>实例的,这些方法抽放为<青霉>回调的并存储在内部 Backbone.history.route 等待执行,的检查Backbone.Router.route code

此操作在当下路由器是实例化完成的,所以你必须间谍您的 Router.method 的实例化引用之前,所以你不得不推迟 Backbone.history.start 间谍后已被激活。

如您必须声明的间谍创建路由器实例之前,你必须做一个的的水平。

说,所以这是我想出了最简单的办法:

 描述(路由器功能(){
  afterEach(函数(){
    Backbone.history.stop();
  });  它(应该叫指数,函数(){
    spyOn(App.Router.prototype,指数)
    VAR路由器=新App.Router();间谍激活后//实例创建
    Backbone.history.start(); //它创建Router实例后启动    router.navigate('',真);    期待(App.Router.prototype.index).toHaveBeenCalled();
  });
});

结论,我觉得 Backbone.Router 的实施还没有一个直观的设计。

Having problems spying method calls on a Backbone Router to ensure it calles the right method on a given route.

excerpt from the test

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

The router

class App.Router extends Backbone.Router
    routes:
        '' : 'index'

    index: ->
        console.log "router.index has been called"

Everything passes except the last test "should call index". It fails with the message "Expected spy index to have been called". Ive tried other variants

it "should call index", ->
    spyOn(@router, "index")
    @router.navigate('', true)
    expect(@router.index).toHaveBeenCalled()

I can also see the "router.index has been called" log output in the test output from the original Router.index function

Thanks!

EDIT: One solution

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

解决方案

It has took too much time to me to come with a working jsFiddle and the question has been already answered by @MarkRushakoff.

Still I have some comments.

The way Backbone is binding the routes make very difficult to test it.

The point is that the router methods are not called directly in the router instance, the methods are taked as callbacks and stored in an internal Backbone.history.route waiting for execution, check the Backbone.Router.route code.

This operation is done in the moment the Router is instantiate, so you have to spy your Router.method before you instantiate the reference, so for you have to delay Backbone.history.start also after the spy has been activated.

As you have to declare the spy before the router instance is created you have to do it in a Class level.

Said so this is the simplest solution I came with:

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

Conclusion, I think the Backbone.Router implementation has not an intuitive design.

这篇关于刺探Backbone.js的路线茉莉调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆