mardi 26 janvier 2016

Stubbing out mongoose required module dependency

I'm trying to use rewire to stub out my stripe dependency on the Order model. I'm currently getting this error:

OverwriteModelError: Cannot overwrite 'Order' model once compiled.

How can I load up my Order module in such a way that I can stub out the stripe module that's being required by Order?

var should = require('should'),
request = require('supertest'),
path = require('path'),
mongoose = require('mongoose'),
Business = mongoose.model('Business'),
rewire = require('rewire'),
//Order = mongoose.model('Order'), // <----- this module uses   var stripe = require('stripe')(SECRET_KEY_HERE);
Order = rewire('../../server/models/order.server.model'),

sinon = require('sinon'),
express = require(path.resolve('./config/lib/express'));
describe('Order CRUD tests', function () {
    beforeEach(function (done) {
        this.stripe = {
            tokens:{
                create: sinon.stub().yields({"id":"token_xyz"})
            },
            charges:{
                create: sinon.stub().yields({"id":"charge_xyz"})
            }

        };

        Order.stripe.__set__("tokens", this.stripe.tokens);
        Order.stripe.__set__("charges", this.stripe.charges);
        done();

    });

});

Aucun commentaire:

Enregistrer un commentaire