問題描述
我有一個表單,它在最終發(fā)布到它的 ACTION URL 之前執(zhí)行了一些廣泛的 Javascript 內(nèi)容.我正在編寫一些 Jasmine 單元測試,并希望確保在提交表單時發(fā)生 Javascript 內(nèi)容.但是,我絕對不希望頁面在我進行單元測試時轉(zhuǎn)到 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 還很陌生,我不確定如何實現(xiàn)它,并且在網(wǎng)絡(luò)上找不到任何相關(guān)的示例.有沒有人有一些我可以查看的示例代碼可以完成我的需要?
...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();
});
我所做的基本上是停止在關(guān)聯(lián)回調(diào)中返回 false 的給定表單的每次提交(請參閱 submitCallback 行為).
What I do is basically stopping every submit for given form returning false in the associated callback (see submitCallback behavior).
那我也可以測試回調(diào)是否被調(diào)用...
Then I can also test callback has been called...
希望對你有幫助!
這篇關(guān)于如何在 Jasmine 中測試表單提交?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!