点击元素的摩卡测试

时间:2013-10-09 07:44:16

标签: ruby-on-rails mocha

我正在为我的Rails应用程序中的JS编写测试,并使用https://github.com/modeset/teaspoon gem来测试js。

Teaspoon使用mocha。我想知道我如何test click function for a button? 以下是要测试的功能:

$(document).on('click', '#import_customs_button', (event)->
    dfd = $.Deferred()
    customs_id = $('#highlight_customs_id').val().trim()
    customs_dfd = $.ajax 'http://api.5min.com/video/' + customs_id + '/info.json'

    customs_dfd.done (result)=>
      if result.api?.exception?
        dfd.reject result.api.exception
      else
        dfd.resolve result

    customs_dfd.fail =>
      dfd.reject "AJAX error"

    dfd.done (result)=>
      obj = if _.isString(result) then $.parseJSON result else result
      _.each obj.items, (item)=>
        seconds = parseInt(item.duration, 10)

        $('#highlight_title').val item.title
        $('#highlight_seo_title').val item.title.match(/[A-Za-z0-9 \-]/g).join ''
        $('#description').val item.description
        $('#highlight-running-time').html formatRunningTime seconds
        if($("#segment_type").html() == "Top Stories")
          $('#highlight_seo_tags').val item.keywords.replace(/,/g, ', ')

    dfd.fail (err)=>
      alert err

    false
  )

0 个答案:

没有答案