我正在使用 qUnit 和 mockjax 来尝试处理简单的异步表单提交,但由于某种原因,异步 POST 似乎通过了 mockjax。
test 'RuleModal closes the modal on a successful form submission event', ->
$.mockjax
dataType: 'json'
url: '/url'
type: 'post'
responseText:
status: 'success'
$dom = $('<div class="show-modal"><form action="/url" method="post"></form></div>')
$form = $dom.find('form')
modal = new RuleModal($dom)
$form.submit()
equal $($dom).hasClass('show-modal'), false, 'closes the modal after form submission'
和实施
_bindSubmit: ->
modal = this
@$modal.find('form').on 'submit', (event) ->
event.preventDefault()
$.ajax
dataType: 'json'
url: @action
type: @method
data: $(this).serialize()
success: (data, status, xhr) ->
modal.close()
error: (xhr, status, error) ->
alert 'Something went wrong: ' + error
我尝试对实现进行硬编码以完全匹配测试,但这仍然不起作用。我究竟做错了什么?