Nodejstesting图像调整与摩卡

我已经使用graphicmagick来调整我的nodejs应用程序中的图像的大小。

问题是编写unit testing的时候,我似乎找不到任何方向或例子。 是否有道理,我testing图像resize,看到我使用第三方模块? 如果是的话,我怎么能写我的代码testing?

 // dependencies var async = require('async'); var AWS = require('aws-sdk'); var gm = require('gm').subClass({ imageMagick: true }); var util = require('util'); // get reference to S3 client var s3 = new AWS.S3(); var _800px = { width: 800, destinationPath: "large" }; var _500px = { width: 500, destinationPath: "medium" }; var _200px = { width: 200, destinationPath: "small" }; var _45px = { width: 45, destinationPath: "thumbnail" }; var _sizesArray = [_800px, _500px, _200px, _45px]; var len = _sizesArray.length; // handler for dev environment exports.GruntHandler = function (filepath) { console.log("Path to file is: " + filepath); // get the file name var srcFile = filepath.split("/").pop(); var dstnFile = "dst"; // Infer the image type. var typeMatch = srcFile.match(/\.([^.]*)$/); if (!typeMatch) { console.error('unable to infer image type for key ' + srcFile); return; } var imageType = typeMatch[1]; if (imageType != "jpg" && imageType != "png") { console.log('skipping non-image ' + srcFile); return; } for (var i = 0; i<len; i++) { // Transform the image buffer in memory. gm(filepath) .resize(_sizesArray[i].width) .write(dstnFile + "/" + _sizesArray[i].destinationPath + "/" + srcFile, function (err) { if (err) { console.log(err); } }); } console.log(" grunt handler called!"); }; 

编写unit testing的惯例是只testing隔离单元。 所有的外部依赖应该被扼杀/嘲弄,所以你只能检查你的单位的逻辑(在这种情况下,你的单位是你的模块)。

至于要testing什么 ,你的单位的唯一的公共方法是“GruntHandler”,所以这是你应该testing的唯一的方法,因为这是该单位提供给其他单位的服务。

为了replace所有模块的依赖关系,我喜欢使用proxyquire包。 它用你可以控制的存根来代替“需要”的呼叫。

我写了一个使用我个人使用的testing堆栈的示例testing:proxyquire,mocha,sinon作为间谍,chai作为断言。

安装程序 – 把它放在一个“testing助手”模块中,要求在所有的testing文件中:

 var chai = require('chai'); var sinonChai = require("sinon-chai"); var expect = chai.expect; var sinon = require('sinon'); chai.use(sinonChai); var proxyquire = require('proxyquire'); 

并在您的testing文件中:

 require('./test-helper'); describe('Image Resizing module', function () { var gmSubclassStub = sinon.stub(); var testedModule = proxyquire('path-to-tested-file', { 'gm': {subClass: sinon.stub().returns(gmSubclassStub)} }); describe.only('GruntHandler', function () { it("should call gm write with correct files", function () { // Arrange var filepath = 'pepo.jpg'; // Spies are the methods you expect were actually called var write800Spy = sinon.spy(); var write500Spy = sinon.spy(); var write200Spy = sinon.spy(); var write45Spy = sinon.spy(); // This is a stub that will return the correct spy for each iteration of the for loop var resizeStub = sinon.stub(); resizeStub.withArgs(800).returns({write:write800Spy}); resizeStub.withArgs(500).returns({write:write500Spy}); resizeStub.withArgs(200).returns({write:write200Spy}); resizeStub.withArgs(45).returns({write:write45Spy}); // Stub is used when you just want to simulate a returned value gmSubclassStub.withArgs(filepath).returns({resize:resizeStub}); // Act - this calls the tested method testedModule.GruntHandler(filepath); // Assert expect(write800Spy).calledWith("dst/large/pepo.jpg"); expect(write500Spy).calledWith("dst/medium/pepo.jpg"); expect(write200Spy).calledWith("dst/small/pepo.jpg"); expect(write45Spy).calledWith("dst/thumbnail/pepo.jpg"); }); }); }); 

更多关于sinon间谍,存根和嘲笑的信息: http ://sinonjs.org/

Proxyquire: https : //github.com/thlorenz/proxyquire

关于这一切的好教程: http : //kroltech.com/2014/02/node-js-testing-with-mocha-chai-sinon-proxyquire/#.VPTS9fmUdV0