I am trying to stub nodejs stripe api using sinon to test client creation using a test that looks like this:
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'}]);
controller.post(req, {}, done);
I understand that I stub.create.yieldsmust call the first callback and pass it (in this case) a null value, followed by an object with the identifier xyz789 Perhaps I'm wrong.
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 the client is undefined.
Did I do something wrong?
EDIT
I think the problem may be here: (striped documents)
var stripe = require('stripe')(' your stripe API key ');
So the constructor stripeaccepts the api key
In my test, I do not supply one: var stripe = require ('stripe');
But in my controller, I have:
var stripe = require('stripe')('my-key-from-config');
So, according to 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'});
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;