很简单的问题:
我希望我们sinon.js测试一段JavaScript,以确保它调用$.ajax
法同时做两件事情:
- 我不想竟然打服务器
- 我想小样来自服务器的响应
所以这里的JS:
$.ajax
url: "/tickets/id.json"
dataType: 'json'
.done (data) =>
HandlebarsTemplates["tickets/popup_title"] data
这是我的测试:
describe 'PopupDisplayer', ->
beforeEach ->
loadFixtures 'popup_displayer'
new PopupDisplayer
@title_stub = sinon.stub( HandlebarsTemplates, "tickets/popup_title")
@jquery_stub = sinon.stub(jQuery, 'ajax').yieldsTo('done', {})
//This triggers the ajax call
$('.popupable .title').mouseenter()
afterEach ->
HandlebarsTemplates['tickets/popup_title'].restore()
HandlebarsTemplates['tickets/popup_content'].restore()
jQuery.ajax.restore()
@server.restore()
it 'renders the title with the data returned from the server', ->
expect(@title_stub).toHaveBeenCalledWith( {})
这个测试失败,虽然有以下情况除外:
TypeError: ajax expected to yield to 'done', but no object with such a property was passed. Received [[object Object]]
所以我想我不知道我是否可以模拟了一个jQuery的请求,以获得能够成功地应对响应.done
通话显然我不明白的defferedObject()
不够好。