mercredi 31 août 2016

Testing callback in class with bind(this)

I m building an application TDD based and I m having a trouble dealing with a test when I m trying to bind a function call inside another function :

/**
   * Loads the client trusted strategy from passport
   */
  apply () {
    passport.use(this.name, new passportStrategy.Strategy(this.callback.bind(this)))
  }

I need to test that the passportStrategy.Strategy as been called with the object callback method.

Here's the test I m having :

it('should call the passport.use method with clientTrusted value and a new Passport strategy object', () => {
      const spy = chai.spy()
      const ClientTrustedProxied = proxyquire('./../../../app/business/strategy/ClientTrusted', {passport: {use: spy}})
      const clientTrusted = new ClientTrustedProxied(null, Constants.STRATEGY_CLIENT_TRUSTED)
      clientTrusted.apply()
      expect(spy).to.have.been.called.with(Constants.STRATEGY_CLIENT_TRUSTED, new Strategy(clientTrusted.callback))
    })

The fact is clientTrusted.callback is not the same as this.callback.bind(this) (apparently) so I can't test the equality of the two elements.

But if I remove the bind(), the test is passing green, but my app doesn't work anymore (super js scoping).

I need a solution, or at least some help, to make that test passing green.

NB : the strategy object in the test is the following one :

import { Strategy } from 'passport-oauth2-public-client'

or in the class (because of conflicted names):

import * as passportStrategy from 'passport-oauth2-public-client'

Thanks for you help

Aucun commentaire:

Enregistrer un commentaire