对象文字会阻塞吗?我正在尝试将其他函数的结果存储在我的分页助手类中,并且我的 Mocha 测试由于超时而失败,因为没有返回回调。如果我local
从所有变量中删除(即local.count
to count
)并删除.then
持有回调并将其放在第一个.then
它突然起作用。
render: (model, query, cb) =>
self = @
count = 0
local = {}
count_promise = @query_count model, query
count_promise.then (c) ->
local.count = c
local.pages = self.pages(count)
local.prev_class = self.prev_class(pages)
local.next_class = self.next_class(pages)
local.prev_page = self.prev_page(pages)
local.next_page = self.next_page(pages)
.then ->
cb({num_pages: local.pages, current_page: self.page, prev_class: local.prev_class, next_class: local.next_class, prev_page: local.prev_page, next_page: local.next_page, results: {}})
.fail (err) ->
throw new Error(err)
return
编辑
我在回调之前添加了 console.log 以查看是否有任何显示。
.then ->
console.log local
cb({num_pages: local.pages, current_page: self.page, prev_class: local.prev_class, next_class: local.next_class, prev_page: local.prev_page, next_page: local.next_page, results: {}})
什么都没遇到。
使用 Sinon 和 Chai 断言的特定方法的 Mocha 测试代码:
describe 'PaginationHelper', ->
describe 'Pagination Helper Initializes', ->
it 'accepts page', ->
assert.ok(new PaginationHelper(2))
describe 'method testing', ->
describe 'render method', ->
ph = new PaginationHelper(2)
result = {}
before (done) ->
ph.render BlogModel, {}, (res) ->
result = res
done()
it 'returns object', ->
assert.isObject(result)
it 'returns object with num_pages', ->
assert.property(result, "num_pages")
it 'returns object with current_page', ->
assert.property(result, 'current_page')
it 'returns object with prev_class', ->
assert.property(result, 'prev_class')
it 'returns object with next_class', ->
assert.property(result, 'next_class')
describe 'render output testing', ->
result = undefined
before (done) ->
ph = new PaginationHelper(1)
ph.render BlogModel, {}, (res) ->
result = res
done()
it 'returns a number for num_pages', ->
assert.isNumber(result.num_pages)
it 'returns a number for current_page', ->
assert.isNumber(result.current_page)
it 'returns string for prev_class', ->
assert.isString(result.prev_class)
it 'returns string for next_class', ->
assert.isString(result.next_class)
此测试的结果输出:
Error: Command failed: ✖ 1 of 35 tests failed:
1) PaginationHelper method testing render method "before all" hook:
Error: timeout of 2000ms exceeded