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

從 Karma/Jasmine 測試加載外部文件

Loading external file from Karma/Jasmine test(從 Karma/Jasmine 測試加載外部文件)
本文介紹了從 Karma/Jasmine 測試加載外部文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試完成 Jasmine 測試(使用 Karma 和 IntelliJ 13)來驗證 JSON 文件.理想情況下,我的測試將簡單地將 JSON 文件加載到數據對象中,然后讓我解析以檢查有效的格式和數據.我不需要在之前或之后驗證函數,也不需要針對服務器進行測試.

I'm trying to accomplish a Jasmine test (using Karma and IntelliJ 13) to validate JSON files. Ideally, my test would simply load a JSON file into a data object, then let me parse through to check for valid formatting and data. I don't need to validate functions before or after, nor do I need to test against a server.

我的基本設置是這樣的:

My basic setup is like this:

it("should load an external file", function(){
var asyncCallComplete, result,
            _this = this;
        // asyncCallComplete is set to true when the ajax call is complete
        asyncCallComplete = false;

        // result stores the result of the successful ajax call
        result = null;

        // SECTION 1 - call asynchronous function
        runs(function() {
            return $.ajax('/test/config.json', {
                type: 'GET',
                success: function(data) {
                    asyncCallComplete = true;
                    result = data;
                },
                error: function() {
                    asyncCallComplete = true;
                }
            });
        });

        // SECTION 2 - wait for the asynchronous call to complete
        waitsFor(function() {
            return asyncCallComplete !== false;
        }, "async to complete");

        // SECTION 3 - perform tests
        return runs(function() {
            return expect(result).not.toBeNull();
        });
}

問題是無論我使用什么路徑,我都會收到 404 錯誤并且文件不會加載.我嘗試使用此測試服務從遠程服務器加載外部 JSON 結果:

The problem is that no matter what path I use, I get a 404 error and the file won't load. I've tried loading an external JSON result from a remote server using this test service:

http://date.jsontest.com/

這行得通.

我的測試文件名為/test/mySpec.js,我的 karma.conf.js 文件位于根目錄中.我已經將 JSON 文件移動到所有這些位置,但沒有運氣.我做錯了什么?

My test file is named /test/mySpec.js and my karma.conf.js file is on the root. I have moved around the JSON file to all of these locations with no luck. What am I doing wrong?

更新答案:

根據下面的答案,我將此添加到我的 karma.conf.js:

Per the answer below, I added this to my karma.conf.js:

// fixtures
{ pattern: 'test/*.json',
    watched: true,
    served:  true,
    included: false
}

然后,我這樣寫了我的測試:

Then, I wrote my test this way:

    var json:any;
    it("should load a fixture", function () {
        jasmine.getFixtures().fixturesPath = "base/test/"
        var f = readFixtures("registration.json");
        json = JSON.parse(f);
        expect(json).toBeDefined();

    })

    it("should have a title", function () {
        expect(json.title).toNotBe(null);
    })
    etc...

它通過了.

推薦答案

您是否通過 karma.config.js 提供 JSON 文件?

Are you serving the JSON file via karma.config.js?

您可以通過夾具提供 JSON 文件:

You can serve JSON files via fixture:

files: [
      // angular 
      'angular.min.js',
      'angular-route.js',
      'angular-mocks.js',

      // jasmine jquery helper
     'jquery-1.10.2.min.js',
     'jasmine-jquery.js',

      //  app
      '../../public/js/app.js',

      // tests
      '*-spec.js',

      // JSON fixture
      { pattern:  '/test/*.json',
        watched:  true,
        served:   true,
        included: false }
    ],

這篇關于從 Karma/Jasmine 測試加載外部文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 天天搞天天操 | 午夜免费视频 | 久久久久91 | 91影片 | 久久精品青青大伊人av | 欧美精品在线看 | 久久久久久高清 | 日韩视频免费 | 国产黄色网| 国产精品中文字幕在线播放 | 97精品国产 | 色婷婷亚洲一区二区三区 | 国产一区二区三区四区五区加勒比 | 一区二区三区免费在线观看 | 精品一二区 | 亚洲天天干 | 国产日韩精品在线 | 国产精品毛片一区二区在线看 | 天天干天天玩天天操 | 视频羞羞| 一级黄在线观看 | 黄色免费网址大全 | 亚洲成av人片在线观看无码 | 天天澡天天狠天天天做 | 欧美一级在线观看 | 天天天插| 精品一区二区三区在线观看 | 亚洲精品一区二区三区蜜桃久 | 亚洲第一视频网站 | 亚洲国产一区在线 | 中文字幕在线观看成人 | 久久免费精彩视频 | 国产精品久久久久久久久久 | 在线欧美一区二区 | 日本天天操| 黄网免费| 精品91视频 | www.青青草| 在线亚洲一区 | 秋霞在线一区 | www.日本国产 |