I'm trying to stub the nodejs stripe api with sinon, to test the creation of a customer using a test that looks like:
var sinon = require('sinon');
var stripe = require('stripe');
var controller = require('../my-controller');
var stub = sinon.stub(stripe.customers, 'create');
stub.create.yields([null, {id: 'xyz789'}]);
//stub.create.yields(null, {id: 'xyz789'}); //same result with or without array
controller.post(req, {}, done);
My understanding is that stub.create.yields
should call the first callback, and pass it (in this case) null, followed by an object with id of xyz789
This may be where I am mistaken
Inside my 'controller' I have the following:
exports.post = function(req, res, next) {
stripe.customers.create({
card: req.body.stripeToken,
plan: 'standard1month',
email: req.body.email
}, function(err, customer) {
console.log('ERR = ', err)
console.log('CUSTOMER = ', customer)
err, and customer are both undefined.
Have I done something wrong?
EDIT
I think the issue could be around here:
(stripe docs)
var stripe = require('stripe')(' your stripe API key ');
So, stripe
constructor takes an api key
In my test, I'm not supplying one:
var stripe = require('stripe');
But in my controller, I have:
var stripe = require('stripe')('my-key-from-config');
So, as per your example, I have:
test.js:
var controller = require('./controller');
var sinon = require('sinon');
var stripe = require('stripe')('test');
var stub = sinon.stub(stripe.customers, 'create');
stub.yields(null, {id: 'xyz789'});
//stub.create.yields(null, {id: 'xyz789'}); //same result with or without array
controller.post({}, {}, function(){});
controller.js
var stripe = require('stripe')('my-key-from-config');
var controller = {
post: function (req, res, done) {
stripe.customers.create({
card: req.body,
plan: 'standard1month',
}, function(err, customer) {
console.log('ERR = ', err);
console.log('CUSTOMER = ', customer);
});
}
}
module.exports = controller;