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

如何提供模擬文件來更改 <input type='fil

How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
本文介紹了如何提供模擬文件來更改 <input type='file'> 的事件用于單元測試的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在進行單元測試時遇到了困難,我想在其中驗證文件的處理,通常可以通過 <input type='file'> 在視圖中選擇該文件.

I'm having difficulties with a unit test in which I want to verify the processing of a file, which would usually be selected in the view via <input type='file'>.

在我的 AngularJS 應用程序的控制器部分,文件在輸入的更改事件中處理,如下所示:

In the controller part of my AngularJS app the file is processed inside the input's change event like so:

//bind the change event of the file input and process the selected file
inputElement.on("change", function (evt) {
    var fileList = evt.target.files;
    var selectedFile = fileList[0];
    if (selectedFile.size > 500000) {
        alert('File too big!');
    // ...

我希望 evt.target.files 在我的單元測試中包含我的模擬數據,而不是用戶選擇的文件.我意識到我自己無法實例化 FileListFile 對象,這將是瀏覽器正在使用的相應對象.因此,我將模擬 FileList 分配給輸入的 files 屬性并手動觸發更改事件:

I'd like evt.target.files to contain my mock data instead of the user's selected file in my unit test. I realized that I can't instantiate a FileList and File object by myself, which would be the according objects the browser is working with. So I went with assigning a mock FileList to the input's files property and triggering the change event manually:

describe('document upload:', function () {
    var input;

    beforeEach(function () {
        input = angular.element("<input type='file' id='file' accept='image/*'>");
        spyOn(document, 'getElementById').andReturn(input);
        createController();
    });

    it('should check file size of the selected file', function () {
        var file = {
            name: "test.png",
            size: 500001,
            type: "image/png"
        };

        var fileList = {
            0: file,
            length: 1,
            item: function (index) { return file; }
        };

        input.files = fileList; // assign the mock files to the input element 
        input.triggerHandler("change"); // trigger the change event

        expect(window.alert).toHaveBeenCalledWith('File too big!');
    });

不幸的是,這會導致控制器中出現以下錯誤,表明此嘗試失敗,因為文件根本沒有分配給輸入元素:

Unfortunately, this causes the following error in the controller which shows that this attempt failed because the files were not assigned to the input element at all:

TypeError: 'undefined' 不是對象(評估 'evt.target.files')

出于安全原因,我已經發現 input.files 屬性是只讀的.所以我開始了另一種方法,通過調度一個定制的更改來提供 files 屬性,但仍然沒有成功.

I already found out that the input.files property is read-only for security reasons. So I started another approach by dispatching a customized change which would provide the files property, but still without success.

長話短說:我很想學習一個可行的解決方案或任何關于如何處理這個測試用例的最佳實踐.

推薦答案

讓我們重新思考 AngularJS,DOM 必須在指令中處理

Let's rethink AngularJS, DOM must be handled in a directive

我們不應該在控制器中處理 DOM 元素,即 element.on('change', ..,尤其是為了測試目的.在控制器中,您與數據交談,而不是與 DOM.

We should not deal with DOM element in a controller, i.e. element.on('change', .., especially for testing purpose. In a controller, You talk to data, not to DOM.

因此,那些 onchange 應該是如下的指令

Thus, those onchange should be a directive like the following

<input type="file" name='file' ng-change="fileChanged()" /> <br/>

但是,不幸的是,ng-change 不適用于 type="file".我不確定未來的版本是否適用于此.我們仍然可以應用相同的方法.

However, unfortunately, ng-change does not work well with type="file". I am not sure that the future version works with this or not. We still can apply the same method though.

<input type="file" 
  onchange="angular.element(this).scope().fileChanged(this.files)" />

在控制器中,我們只是簡單地定義一個方法

and in the controller, we just simply define a method

$scope.fileChanged = function(files) {
  return files.0.length < 500000;
};

現在,一切都只是普通的控制器測試.不再需要處理 angular.element$compiletriggers 等!:)

Now, everything is just a normal controller test. No more dealing with angular.element, $compile, triggers, etc.! :)

describe(‘MyCtrl’, function() {
  it('does check files', inject(
    function($rootScope, $controller) {
      scope = $rootScope.new();
      ctrl = $controller(‘UploadCtrl’, {‘$scope’: scope});

      var files = { 0: {name:'foo', size: 500001} };
      expect(scope.fileChanged(files)).toBe(true);
    }
  ));
});

http://plnkr.co/edit/1J7ETus0etBLO18FQDhK?p=preview

這篇關于如何提供模擬文件來更改 &lt;input type='file'&gt; 的事件用于單元測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 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 - 如何監視函數中的函數調用?)
jasmine test fails with undefined is not a function(evaluating $browser.$$checkUrlChange())(茉莉花測試失敗,未定義不是函數(評估 $browser.$$checkUrlChange()))
主站蜘蛛池模板: 亚洲一区二区久久久 | 日韩精品视频中文字幕 | 日韩久久网 | 亚洲男人的天堂网站 | 瑞克和莫蒂第五季在线观看 | 国产精品美女久久久 | 999久久久精品 | www.887色视频免费 | 久久久蜜桃 | 亚洲黄色片免费观看 | 一区二区三区在线免费 | 黄色一级大片在线观看 | 日韩av黄色| 成人免费视频播放 | 欧美电影在线 | 尤物视频在线免费观看 | 精品一区二区三区在线观看国产 | 亚洲一区二区三区福利 | 黄色片在线网站 | 久久久久久久一区 | 精品99久久久久久 | 亚洲国产精品一区二区久久 | 天天干天天操天天爽 | 久久av资源网 | 欧美在线视频网 | 日韩欧美国产一区二区三区 | 天天操天天干天天爽 | 亚洲成人精品一区 | 毛片在线免费 | 国产免费一区 | www.嫩草 | 日韩精品视频网 | 国产不卡一区 | 久久av一区二区三区 | 在线观看中文字幕 | www.久久99| 九色 在线 | 九九99久久 | 欧美淫片 | 国产精品久久久免费 | 免费福利视频一区二区三区 |