node.js - Stubbing with proxyquire -


how stub following module proxyquire , sinon:

var email = require("emailjs").server.connect.send(); 

i did following, not working, because when try trigger error within send() still sends email.

sendmailstub = sinon.stub(email, "send");     testedmodule = proxyquire('../index.js', {                 'email': {                     'server': {                         'send': sendmailstub                         }                     }             }); 

and tried:

testedmodule = proxyquire('../index.js', {             email: {send: sendmailstub}         }); 

this full test far, fails:

var chai = require('chai'); var sinonchai = require("sinon-chai"); var sinon = require('sinon'); chai.use(sinonchai); var proxyquire = require('proxyquire'); var testedmodule; var expect = chai.expect;    describe('invoicer', function () {      var nock = require('nock');      var responseoptions = {         username: "peter pan",         user_address_line_1: "never never land",         user_address_line_2: "tree-house 99",         user_post_code: "e4 9by",         delivery_address_line_1: "hook's boat",         delivery_address_line_2: "dock 69",         delivery_post_code: "se2 4c",         order_number: "234234234",         order_date: "20/12/2090",         dispatch_date: "20/12/2090",         items: [             {                 product_name: "fairy dust",                 brand: "airy fairy",                 quantity: 5,                 total: 2000             },             {                 product_name: "pirate sword",                 brand: "pirate's bay",                 quantity: 8,                 total: 2000             }         ],         grand_total: 4000,         user_email: "peter@flyaway.com"     }      var mailoptions = {         text: "hello world"     }      var scope = nock("http://somewhere.com")         .get("/orders")         .reply(200, responseoptions);      var sendstub, readfilestub, url, contextdonespy, obj, connectstub;      before(function () {          readfilestub = sinon.stub();         sendstub = sinon.stub().withargs(mailoptions).callsargwith(1, null, contextdonespy);         connectstub = sinon.stub().returns({             send: sendstub         });          testedmodule = proxyquire('../index', {             'fs': {readfile: readfilestub},             'emailjs': {                 'server': {                     'connect': connectstub                 }             }         });          url = "http://somewhere.com/orders";         contextdonespy = sinon.spy();         obj = {             user: 'akiajmhsjrrygkte4toq',             password: 'ag3nkpej8dxz4dwyz2in/x8kuhn7lh/bqximb0+i+dwy',             host: "email-smtp.eu-west-1.amazonaws.com",             port: 587,             tls: true,             ssl: false         }          readfilestub.withargs('./email.html').callsargwith(1, null, 'file1');          connectstub();     });      it("readfile , successful context.done called", function (done) {         testedmodule.invoicer(url, obj, { done: function () {             contextdonespy.apply(null, arguments);             expect(readfilestub).has.been.called;             expect(contextdonespy).to.have.been.called;             done();         }});      }); }); 

let's first fix line described emailjs using documentation

require("emailjs").server.connect({   /* required server options */ }).send(function (err, message) {   // function callback node convention signature }); 

connect function , should receive parameters, send doesn't return thing, call callback

proxyquire requires use same module name, email should emailjs, on other hand you're wrong assigning stub in proxiquire, should be

connectstub = sinon.stub().returns(sendstub); sendmailstub = sinon.stub(email, "send");     testedmodule = proxyquire('../index.js', {             'emailjs': {                 'server': {                     'connect': connectstub                 }         }); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -