如何测试 Jasmine 中提交的表单?
我有一个表单,它在最终发布到它的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这个代码片段可以帮助你......
我所做的基本上是停止给定表单的每次提交,在关联的回调中返回 false (请参阅submitCallback 行为)。
然后我还可以测试回调已被调用...
希望它有帮助!
Maybe this code snippet can help you...
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!
如果您不想测试是否调用了
submit
,则无需创建间谍。使用普通的jQuery,你也可以达到不提交表单的效果。我将以下代码放在所有测试之外,这样它就适用于文件中的所有测试。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.