nested expect() function with Jasmine BDD does not evaluate
I have the following Jasmine context and assertion:
it "should return a javascript file", ->
# We make a request to /x.js
request {uri: 'http://localhost:3000/x.js'}, (err, res, body) ->开发者_JS百科
expect(res.statusCode).toEqual 200
When I call Jasmine to evaluate the spec, it does not pickup the assertion. How can I make it pickup the assertion?
Your it()
is exiting before your request returns a response. With jasmine, you have to think about asynchronous events a bit harder.
Check out waitsFor()
and do something like this, which will prevent your spec from exiting until the callback is executed, or the default timeout period has elapsed.
it "should return a javascript file", ->
responded = no
request {uri: 'http://localhost:3000/x.js'}, (err, res, body) ->
responded = yes
expect(res.statusCode).toEqual 200
waitsFor -> responded
This will also cause your spec to fail if the server times out, because each waitsFor()
expects to be satisfied eventually.
精彩评论