繁体   English   中英

我该如何对node.js中的文件流进行单元测试?

[英]How would I go about unit testing something with file streams in node.js?

我的源代码是:

var _ = require('lodash');
var fs = require('fs');
var csv = require('fast-csv');
var topPostStream = fs.createWriteStream('top_posts.csv');
var topOtherStream = fs.createWriteStream('other_posts.csv');
var topCsvStream = csv.createWriteStream();
var otherCsvStream = csv.createWriteStream();
var dailyTops = {};

var headerRow = [];

topCsvStream.pipe(topPostStream);
otherCsvStream.pipe(topOtherStream);

console.log(process.argv);

csv
 .fromPath('posts.csv')
 .on('data', function(rowData) {
   if(headerRow.length === 0) {
     // Save header row in CSV's
     headerRow = rowData;
     topCsvStream.write(headerRow);
     otherCsvStream.write(headerRow);
     return;
   }
   if(rowData[2] === 'public' && rowData[5] > 10 && rowData[4] > 9000 && rowData[1].length < 40) {
     // Save to the top_posts file
     topCsvStream.write(rowData);
   } else {
     // Save to the other_posts file
     otherCsvStream.write(rowData);
   }

   // Save to the daily tops
   var postDate = new Date(rowData[6]);
   postDate = new Date(postDate.getYear(), postDate.getMonth(), postDate.getDate());

   if(!dailyTops[postDate] || parseInt(rowData[3]) > parseInt(dailyTops[postDate][3])) {
     dailyTops[postDate] = rowData;
   }
 })
 .on('end', finishWrite);

 function finishWrite() {
   topCsvStream.end();
   otherCsvStream.end();
   writeDailyList(dailyTops, headerRow);
 }

 function writeDailyList(dailyTops, headerRow) {
   var daily = _.map(_.keys(dailyTops), function(key) {
     return dailyTops[key];
   });
   var daily = [headerRow].concat(daily);
   csv
   .writeToPath('daily_top_posts.csv', daily, {headers: true})
 }

我需要编写单元测试来获得接近100%的覆盖率。 我的问题是(a)测试似乎太简单了;(b)我将如何实际测试单元测试?

发布我的评论作为扩展答案。

首先,您需要将数据句柄函数提取到另一个模块中,例如data-handler.js (这是您的代码的示例,可以对其进行增强以使其看起来更好,但只是给出了一个主意)

module.exports = {
   handler: (rowData, topCsvStream, otherCsvStream) => {
      if(headerRow.length === 0) {
         // Save header row in CSV's
         headerRow = rowData;
         topCsvStream.write(headerRow);
         otherCsvStream.write(headerRow);
         return;
      }
      if(rowData[2] === 'public' && rowData[5] > 10 && rowData[4] > 9000 && rowData[1].length < 40) {
        // Save to the top_posts file
        topCsvStream.write(rowData);
      } else {
        // Save to the other_posts file
        otherCsvStream.write(rowData);
      }

      // Save to the daily tops
      var postDate = new Date(rowData[6]);
      postDate = new Date(postDate.getYear(), postDate.getMonth(), postDate.getDate());

     if(!dailyTops[postDate] || parseInt(rowData[3]) >    parseInt(dailyTops[postDate][3])) {
        dailyTops[postDate] = rowData;
      }
   },

   end: (topCsvStream, otherCsvStream) => {
      topCsvStream.end();
      otherCsvStream.end();
      writeDailyList(dailyTops, headerRow);
   }
}

在主文件中,您将引用模块:

const handler = require("./data-handler");

csv
 .fromPath('posts.csv')
 .on("data", (data) => handler.handler(data, topCsvStream, otherCsvStream).on("end", () => handler.end(topCsvStream, otherCsvStream));

现在,您的处理程序代码不再绑定到流,并且无需它们就可以轻松对其进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM