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

如何在 Jasmine 中編寫 FileReader 測(cè)試?

How do I write FileReader test in Jasmine?(如何在 Jasmine 中編寫 FileReader 測(cè)試?)
本文介紹了如何在 Jasmine 中編寫 FileReader 測(cè)試?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

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

我正在嘗試進(jìn)行此測(cè)試,但我不知道如何使用 FileReader 編寫測(cè)試.這是我的代碼

<上一頁(yè)><代碼>功能上傳器(文件){this.file = 文件;}Uploader.prototype = (function() {函數(shù)上傳文件(文件,文件內(nèi)容){var file_data = new FormData()file_data.append('文件名', 文件名)file_data.append('mimetype', file.type)file_data.append('data', file_contents)file_data.append('size', file.size)$.ajax({url: "/上傳/文件",類型:發(fā)布",數(shù)據(jù):文件內(nèi)容,內(nèi)容類型:文件類型,成功:函數(shù)(){//$("#thumbnail").attr("src", "/upload/thumbnail");},錯(cuò)誤:函數(shù)(){警報(bào)(失敗");},xhr:函數(shù)(){myXhr = $.ajaxSettings.xhr();如果(myXhr.upload){myXhr.upload.addEventListener('progress',showProgress, false);} 別的 {console.log("不支持上傳進(jìn)度");}返回我的Xhr;}});}返回 {上傳:函數(shù)(){var self = 這個(gè),閱讀器=新文件閱讀器(),文件內(nèi)容 = {};reader.onload = 函數(shù)(e){file_content = e.target.result.split(',')[1];上傳文件(self.file,文件內(nèi)容);}}};})();

這是我的測(cè)試

<上一頁(yè)><代碼>描述(上傳者",函數(shù)(){it("應(yīng)該上傳文件成功", function() {spyOn($, "ajax");var fakeFile = {};var uploader = new Uploader(fakeFile);上傳者.上傳();期望($.ajax.mostRecentCall.args[0]["url"]).toEqual("/upload/file");})});

但它永遠(yuǎn)不會(huì)到達(dá) reader.onload.

解決方案

這里的問(wèn)題是 reader.onload 的使用很難測(cè)試.您可以使用 reader.addEventListener 代替,這樣您就可以監(jiān)視全局 FileReader 對(duì)象并返回一個(gè)模擬:

eventListener = jasmine.createSpy();spyOn(window, "FileReader").andReturn({添加事件監(jiān)聽器:事件監(jiān)聽器})

然后你可以自己觸發(fā) onload 回調(diào):

expect(eventListener.mostRecentCall.args[0]).toEqual('load');eventListener.mostRecentCall.args[1]({目標(biāo):{result:'你要測(cè)試的結(jié)果'}})

I'm trying to make this test work, but I couldn't get my head around how to write a test with FileReader. This is my code


function Uploader(file) {
    this.file = file;
}

Uploader.prototype =  (function() {

    function upload_file(file, file_contents) {
        var file_data = new FormData()
        file_data.append('filename', file.name)
        file_data.append('mimetype', file.type)
        file_data.append('data', file_contents)
        file_data.append('size', file.size)

        $.ajax({
            url: "/upload/file",
            type: "POST",
            data: file_contents,            
            contentType: file.type,
            success: function(){

                // $("#thumbnail").attr("src", "/upload/thumbnail");    

            },
            error: function(){
                alert("Failed");
            },
            xhr: function() {
                myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){
                    myXhr.upload.addEventListener('progress',showProgress, false);
                } else {
                    console.log("Upload progress is not supported.");
                }
                return myXhr;
            }
        });
    }

    return {
        upload : function() {
            var self = this,
                reader = new FileReader(),
                file_content = {};

            reader.onload = function(e) {
                file_content = e.target.result.split(',')[1];

                upload_file(self.file, file_content);
            }
        }
    };
})();



And this is my test


describe("Uploader", function() {
    it("should upload a file successfully", function() {
        spyOn($, "ajax");
        var fakeFile = {};

        var uploader = new Uploader(fakeFile);
        uploader.upload();

        expect($.ajax.mostRecentCall.args[0]["url"]).toEqual("/upload/file");
    })
});

But it never gets to reader.onload.

解決方案

The problem here is the use of reader.onload which is hard to test. You could use reader.addEventListener instead so you can spy on the global FileReader object and return a mock:

eventListener = jasmine.createSpy();
spyOn(window, "FileReader").andReturn({
 addEventListener: eventListener
})

then you can fire the onload callback by yourself:

expect(eventListener.mostRecentCall.args[0]).toEqual('load');
eventListener.mostRecentCall.args[1]({
  target:{
    result:'the result you wanna test'
  }
})

這篇關(guān)于如何在 Jasmine 中編寫 FileReader 測(cè)試?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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(如何提供模擬文件來(lái)更改 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)用?)
主站蜘蛛池模板: 免费看欧美一级片 | 日韩视频在线观看一区二区 | 日韩1区| 视频在线亚洲 | 欧美综合一区 | 91在线视频免费观看 | 91精品国产综合久久福利软件 | 亚洲国产高清高潮精品美女 | 手机av网 | 91久久精品一区二区二区 | 亚洲 欧美 综合 | 欧美v免费| 91中文在线观看 | 精品国产一区二区国模嫣然 | 91精品国产综合久久久久久 | 国产精品视频网 | 91精品国产综合久久福利软件 | 欧美aⅴ | 亚洲天堂久久新 | 国产精品免费一区二区三区四区 | 中文字幕一区二区三区精彩视频 | 亚洲不卡在线观看 | 久操伊人 | 日韩精品1区2区 | 欧美日韩一区二区在线观看 | 玖玖免费 | 国产男女精品 | 日韩成人免费视频 | 久久国产精品-久久精品 | av在线免费观看网址 | 日韩精品一区二区三区在线观看 | 日本三级在线视频 | 久久丝袜视频 | 国产精品国产馆在线真实露脸 | 亚洲免费在线观看av | 亚洲精品91 | 欧美在线色 | 二区在线观看 | 亚洲精品www久久久久久广东 | 日本不卡在线观看 | 欧美激情久久久 |