Reputation: 415
I was bumping my had against a wall whit unit testing class method that returns promise. So I tried to comment the logic and simply make the method to return new Promise that calls resolve. Still no luck ... So When I pass a function that simply calls done() to .then all is ok. But when I pass function that tests if 3 is 4 and then calls done() it timeouts.
This is my "promise" method:
class Session
constructor: (@ip) ->
@map = []
@get = {}
add: (doc, index = -1) ->
@get[doc.key] = JSON.parse doc.value
if index is -1
@map.push doc
else
@map[index] = doc
ready: ->
new Promise (resolve, reject) -> resolve()
#store.find ip: @ip, (err, docs) =>
# if successful err, docs then resolve()
# else reject err
....
And this are my 2 unit tests (first passes, second exeeds time limit) :
describe "ready", ->
session = undefined
beforeEach ->
Session = proxyquire "./../Session", "./sessionModel":mockSessionModel
session = new Session "ip"
it "simply calls done()", (done) ->
session.ready().then -> done()
it "make a fail assertaion", (done) ->
session.ready().then ->
expect(3).to.equal(4)
done()
Upvotes: 0
Views: 99
Reputation: 415
Yea I found that the hard way, any way I found how to test my code. I wasn't mocking my mongoose DB model right by using proxyquire. Using proxyquire.noCallThru() beforeEach and proxyquire.callThru() afterEach fixed my issue wich in fact was that I had never actually mocked my model since it required mongoose ...
Upvotes: 0
Reputation: 203359
The issue is that for your last test, the code will never get to calling done()
because the assertion before it will fail and throw an exception.
Since Mocha has built-in support for promises, you can rewrite your tests accordingly (without the done
callback):
it "simply calls done()", () ->
return session.ready()
it "make a fail assertion", () ->
return session.ready().then ->
expect(3).to.equal(4)
Upvotes: 2