久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

正確測(cè)試backbone.js中的路由器?

Testing routers in backbone.js properly?(正確測(cè)試backbone.js中的路由器?)
本文介紹了正確測(cè)試backbone.js中的路由器?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

所以我剛剛開始使用 sinon.js & 為我正在進(jìn)行的 javascript 應(yīng)用程序編寫測(cè)試.jasmine.js.總體上運(yùn)行良好,但我還需要能夠測(cè)試我的路由器.

So I've just started to write tests for my in-progress javascript app, using sinon.js & jasmine.js. Works pretty well overall, but I need to also be able to test my routers.

路由器在其當(dāng)前狀態(tài)下將觸發(fā)許多視圖和其他內(nèi)容,通過調(diào)用依賴于應(yīng)用程序的 Backbone.navigate 來終止當(dāng)前的 jasmine.js 測(cè)試狀態(tài)和 UI 迭代.

The routers, in their current state, will trigger an number of views and other stuff, terminating the current jasmine.js test by invoking Backbone.navigate dependent on application state and UI itneraction.

那么我怎樣才能測(cè)試到不同位置的路由是否可行,同時(shí)保持路由器沙盒"并且不允許它們更改路由?

So how could I test that routing to different locations would work, while keeping the routers "sandboxed" and not allowing them to change route?

我可以設(shè)置某種模擬函數(shù)來監(jiān)控 pushState 變化或類似情況嗎?

Can I set up some sort of mock function that will monitor pushState changes or similar?

推薦答案

這是我最終使用自己的.我通過擴(kuò)展它并用空白方法覆蓋方法來制作路由器的模擬版本,以防止它在被調(diào)用時(shí)調(diào)用任何進(jìn)一步的邏輯:

Here's what I ended up using myself. I made a mock version of the router by extending it and overriding the methods with a blank method to prevent it from invoking any further logic when being called:

describe("routers/main", function() {

    beforeEach(function() {

        // Create a mock version of our router by extending it and only overriding
        // the methods
        var mockRouter = App.Routers["Main"].extend({
            index: function() {},
            login: function() {},
            logoff: function() {}
        });

        // Set up a spy and invoke the router
        this.routeSpy = sinon.spy();
        this.router = new mockRouter;

        // Prevent history.start from throwing error
        try {
            Backbone.history.start({silent:true, pushState:true});
        } catch(e) {

        }

        // Reset URL
        this.router.navigate("tests/SpecRunner.html");
    });

    afterEach(function(){
        // Reset URL
        this.router.navigate("tests/SpecRunner.html");
    });

    it('Has the right amount of routes', function() {
        expect(_.size(this.router.routes)).toEqual(4);
    });

    it('/ -route exists and points to the right method', function () {
        expect(this.router.routes['']).toEqual('index');
    });

    it("Can navigate to /", function() {
        this.router.bind("route:index", this.routeSpy);
        this.router.navigate("", true);
        expect(this.routeSpy.calledOnce).toBeTruthy();
        expect(this.routeSpy.calledWith()).toBeTruthy();
    });

});

請(qǐng)注意,上面使用 sinon.js 來創(chuàng)建 spy,與 underscore.js 一起提供 size 功能.

Note that sinon.js is used above to create the spy, along with underscore.js to provide the size function.

這篇關(guān)于正確測(cè)試backbone.js中的路由器?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測(cè)試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運(yùn)行和等待實(shí)際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測(cè)試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測(cè)試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測(cè)試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
主站蜘蛛池模板: 亚洲巨乳自拍在线视频 | 成人国产精品免费观看视频 | 久久久久国产一区二区三区不卡 | 最新黄色在线观看 | 日韩一级 | 黄网免费 | 国产激情小视频 | 91精品国产91综合久久蜜臀 | 亚洲欧美一区二区在线观看 | 在线播放中文字幕 | 日韩视频一级 | 国产视频一区二区 | 99热热99 | 一本一道久久a久久精品综合 | 91福利在线观看视频 | av电影一区 | 成人性视频在线播放 | 久久久久久久久国产 | 最新中文字幕在线播放 | 日日摸夜夜爽人人添av | 亚洲一级毛片 | 日韩在线资源 | 超碰97免费在线 | 天天草天天射 | 国产精品99久久久久久www | 欧美成人一区二区 | 精品美女在线观看 | 亚洲黄色片免费观看 | 日韩在线播放一区 | 亚洲 中文 欧美 日韩 在线观看 | 色就干 | av在线免费播放 | 精品久久国产 | 在线观看精品 | 一级片在线观看 | 亚洲高清在线 | 日韩欧美在线视频观看 | 国产精品视频一区二区三 | 国产精品一卡二卡三卡 | 免费在线观看成人 | 午夜久草 |