admin管理员组文章数量:1178553
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:
...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!
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!
Share Improve this question asked Sep 14, 2011 at 14:41 gcdevgcdev 1,5263 gold badges18 silver badges33 bronze badges2 Answers
Reset to default 41Maybe 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!
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;
});
本文标签: javascriptHow do I test a form submit in JasmineStack Overflow
版权声明:本文标题:javascript - How do I test a form submit in Jasmine? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738013120a2049253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论