2016-11-05 4 views
0

私のソースコードは次のとおりです。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)単体テストを実際に測定するにはどうすればいいのだろうか?

+0

あなたができることは他のモジュールにコードを抽出し、ストリームをモックすることなくテストすることです –

+0

おそらくもっと具体的になりますか? – Shamoon

答えて

1

私のコメントを拡張答えとして投稿する。

まず、データハンドル関数を別のモジュール、例えば、別のモジュールに抽出する必要があります。 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)); 

ハンドラコードはストリームにバインドされていないので、ハンドラコードなしで簡単にテストできます。

関連する問題