Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I stub a function that being promisify'd by bluebird? #200

Closed
stringbeans opened this issue Mar 10, 2017 · 2 comments
Closed

How do I stub a function that being promisify'd by bluebird? #200

stringbeans opened this issue Mar 10, 2017 · 2 comments

Comments

@stringbeans
Copy link

Hi!

I have a function that does something like this...

function someFunction(sqs, message) {
  const sendMessage = Promise.promisify(sqs.sendMessage.bind(sqs));
  return sendMessage(message)
}

How would I setup a test such that i can verify whether sendMessage is called correctly? I understand I can just pass the "promisified" version of sendMessage into someFunction and that eases the stubbing, but it makes my code a little messy.

As for the signature of sqs.sendMessage it would be (message, callbackFn)

Thanks in advance!

@searls
Copy link
Member

searls commented Mar 15, 2017

Since the promisification is an implementation detail of the function, then the public contract between it and the test is that sqs should be passed something that expects the callback. As a result, I would pass it a test double wired to respond to a callback and verify that it returns a promise that resolves to the expected value:

// assuming you have a done() callback or something
var sqs = td.object(['sendMessage'])
td.when(sqs.sendMessage('lol')).thenCallback(null, 'kek')

var result = someFunction(sqs, 'lol')

result.then(function(result) { 
  assert.equal(result, 'kek')
  done()
})

@searls searls closed this as completed Mar 15, 2017
@stringbeans
Copy link
Author

@searls thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants