Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
131
rated 0 times [  135] [ 4]  / answers: 1 / hits: 15703  / 7 Years ago, wed, may 24, 2017, 12:00:00

How do you mock an outer method calling a callback using sinon?
Example given the following code, getText should return 'a string' as response in the callback function



sinon.stub(a, 'getText').returns('a string')
let cb = function(err, response) {
console.log(response)
}
a.getText('abc', cb)


and it should produce the output 'a string' since it calls callback function cb but there is no output


More From » sinon

 Answers
1

You can use callsArgWith


sinon.stub(a, 'getText').callsArgWith(1, null, 'a string')
let cb = function(err, response) {
console.log(response); // 'a string'
}

a.getText('abc', cb)

[#57682] Monday, May 22, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
taylert

Total Points: 627
Total Questions: 91
Total Answers: 108

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
taylert questions
;