2

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

茶匙用途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
  )
4

0 回答 0