如何在javascript单元测试期间加载二进制文件?(How do I load a binary file during javascript unit test?)

编程入门 行业动态 更新时间:2024-10-27 18:18:37
如何在javascript单元测试期间加载二进制文件?(How do I load a binary file during javascript unit test?)

在我的应用程序中,用户使用HTML5拖放来处理二进制文件。 这部分代码运行正常。 在chrome中,我拖动一个二进制文件并使用FileReader创建一个arrayBuffer。 这一切似乎都很好。 我正在为这个功能编写测试,我很茫然。 如何将二进制文件加载到单元测试中? 对于我正在测试的代码,我只需要一个arrayBuffer。 目前,我手动创建了arrayBuffer,但这不是一个可持续的解决方案。 为了使我的测试有效,我需要能够随时抛出一个新的二进制文件并进行新的测试。 我的测试环境是testacular + jasmine。

( function() {"use strict"; function loadSimpleDataView() { //instead of defining ArrayBuffer, //I want it to be generated based upon an external file var buffer = new ArrayBuffer(4), dataView = new DataView(buffer), int8View = new Int8Array(buffer); int8View.set([0x00,0x01,0x02,0x03]); return dataView; } describe('mymodule', function() { it('mytest', function() { var dataView = loadSimpleDataView(); expect(dataView).toBeDefined(); //do rest of tests }); }); }());

In my application, the user uses HTML5 drag and drop to process a binary file. That part of the code works fine. In chrome, I drag a binary file and use a FileReader to create an arrayBuffer. That all seems to work fine. I am writing tests for this functionality and I am at a loss. How do I load a binary file into my unit test? For the bit of code I am testing, I only need an arrayBuffer. Currently, I am creating the arrayBuffer manually, but that isn't a sustainable solution. In order for my tests to be effective, I need to be able to throw a new binary file in at anytime and make a new test. My testing environment is testacular+jasmine.

( function() {"use strict"; function loadSimpleDataView() { //instead of defining ArrayBuffer, //I want it to be generated based upon an external file var buffer = new ArrayBuffer(4), dataView = new DataView(buffer), int8View = new Int8Array(buffer); int8View.set([0x00,0x01,0x02,0x03]); return dataView; } describe('mymodule', function() { it('mytest', function() { var dataView = loadSimpleDataView(); expect(dataView).toBeDefined(); //do rest of tests }); }); }());

更多推荐

本文发布于:2023-07-20 20:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   加载   二进制文件   如何在   unit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!