如何在 Jasmine 中测试表单提交?

How do I test a form submit in Jasmine?(如何在 Jasmine 中测试表单提交?)
本文介绍了如何在 Jasmine 中测试表单提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个表单,它在最终发布到它的 ACTION URL 之前执行了一些广泛的 Javascript 内容.我正在编写一些 Jasmine 单元测试,并希望确保在提交表单时发生 Javascript 内容.但是,我绝对不希望页面在我进行单元测试时转到 ACTION URL.

I have a form that does some extensive Javascript stuff before finally POSTing to it's ACTION URL. I am writing some Jasmine unit tests and want to make sure the Javascript stuff happens when the form is submitted. However, I definitely don't want the page to go to the ACTION URL while I am unit testing.

我在这里看到了一个不错的建议:http://groups.google.com/group/jasmine-js/browse_thread/thread/a010eced8db17c1a?pli=1

I saw what seemed like a good suggestion here: http://groups.google.com/group/jasmine-js/browse_thread/thread/a010eced8db17c1a?pli=1

...但是,由于我对 Jasmine 还很陌生,我不确定如何实现它,并且在网络上找不到任何相关的示例.有没有人有一些我可以查看的示例代码可以完成我的需要?

...but, as I am fairly new to Jasmine, I am unsure how to implement it and cannot find any pertinent examples on the web. Does anyone have some sample code I could look at that would accomplish what I need?

谢谢!

推荐答案

也许这个代码片段可以帮助你...

Maybe this code snippet can help you...

it('calls ajax post on export button click', function() {
  view.render();
  var form = $('#export_images_xml_form');
  var submitCallback = jasmine.createSpy().andReturn(false);
  form.submit(submitCallback);

  $('#export_images_xml_button').click();

  expect(form.attr('action')).toEqual('/export');
  expect($('#export_images_xml_form input').attr('value')).toEqual('22,33,44');
  expect(submitCallback).toHaveBeenCalled();
});

我所做的基本上是停止在关联回调中返回 false 的给定表单的每次提交(请参阅 submitCallback 行为).

What I do is basically stopping every submit for given form returning false in the associated callback (see submitCallback behavior).

那我也可以测试回调是否被调用...

Then I can also test callback has been called...

希望对你有帮助!

这篇关于如何在 Jasmine 中测试表单提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Drag amp; Drop with Protractor by Repeater(拖动amp;通过中继器使用量角器掉落)
Getting the position of the element in a list when it#39;s drag/dropped (ui.sortable)(拖放时获取元素在列表中的位置(ui.sortable))
Detecting HTML5 Drag And Drop support in javascript(在 javascript 中检测 HTML5 拖放支持)
HTML5 drop event doesn#39;t work unless dragover is handled(除非处理了拖动,否则 HTML5 放置事件不起作用)
How to use jQuery#39;s drop event to upload files dragged from the desktop?(如何使用 jQuery 的 drop 事件上传从桌面拖动的文件?)
Drop image into contenteditable in Chrome to the cursor(将图像拖放到 Chrome 中的 contenteditable 到光标处)