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

发布于 2024-12-04 16:46:51 字数 466 浏览 0 评论 0原文

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

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

...但是,由于我对 Jasmine 相当陌生,我不确定如何实现它,但在网络上找不到任何相关示例。有谁有一些示例代码我可以看一下来完成我所需要的吗?

谢谢!

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.

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

...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?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多孤肩上扛 2024-12-11 16:46:51

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

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 行为)。

然后我还可以测试回调已被调用...

希望它有帮助!

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();
});

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...

Hope it helps!

水晶透心 2024-12-11 16:46:51

如果您不想测试是否调用了 submit,则无需创建间谍。使用普通的jQuery,你也可以达到不提交表单的效果。我将以下代码放在所有测试之外,这样它就适用于文件中的所有测试。

$('form').on('submit', function() {
    return false;
});

If you don't want to test that submit was called, you don't need to create a spy. With plain jQuery, you can also attain the effect of not submitting the form. I put the following code outside of all tests, that way it applies to all tests within the file.

$('form').on('submit', function() {
    return false;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文