Rinsing strips with synon - use of caps.

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'}]);
//stub.create.yields(null, {id: 'xyz789'}); //same result with or without array 

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'});
//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;
+4
3

:

var stripe = require('stripe')('my-key-from-config');

customer . , :

test.js

var stripe = require('stripe')('test');
var stub = sinon.stub(stripe.customers, 'create');

stripe :

controller.js

var stripe = require('stripe')('my-key-from-config');
var controller = { ... }

.

SO....

stripe , , nock, http, :

  nock('https://api.stripe.com:443')
    .post('/v1/customers', "email=user1%40example.com&card=tok_5I6lor706YkUbj")
    .reply 200, 
      object: 'customer'  
      id: 'cus_somestripeid'
+14

, #post() # stripe.customers.create() API- Stripe. @lambinator , ,

require('stripe')('my-key-from-config')

require('stripe')('test')

# stripe.customers.create() .

, @lambinator. - , - . , "- " (, ), . , Stripe:

Stripe.js:

...

// expose constructor as a named property to enable mocking with Sinon.JS
module.exports.Stripe = Stripe;

:

var sinon = require('sinon');
var stripe = require('stripe')('test');
var StripeObjectStub = sinon.stub(Stripe, 'Stripe', function(){
  return stripe;
});
//NOTE! This is required AFTER we've stubbed the constructor.
var controller = require('./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({}, {}, function(){});

:

require('stripe').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);
    });
}

# stripe.customers.create() .

+4

, , .

yields callsArg, , , Function.prototype.apply - , @psquared , , .

. JSFiddle, , .

var stripe = {
    customers: {
        create: function () {}
    }
};
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);
        });
    }
}

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(){});

, , , .

+1

Source: https://habr.com/ru/post/1533405/


All Articles