我在系統生命週期中反向工作。幾個月前,我寫了一個大型的JavaScript庫。然後我必須做到這一切客觀,現在,我必須爲它編寫單元測試。我正在使用Maven,並在我的pom.xml中有jasmine-maven-plugin
。我遇到的問題是我應該寫什麼測試,以及有多少。我應該在Jasmine中編寫單元測試?
第一個例子很簡單。該函數接受一個字符串並返回大寫的第一個字母。
var toolsFn = {
capitaliseFirstLetter: function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
},
等我單位測試:
describe("toolsFn - capitaliseFirstLetter", function() {
it("capitalises the first letter of a given string", function() {
expect(toolsFn.capitaliseFirstLetter("hello World!")).toBe("Hello World!");
});
});
不過,我不確定我應該爲我的許多其他方法做什麼。他們中的大多數處理html代碼,例如更改選項卡,顯示通知,禁用/啓用控件。我應該只是期望方法toHaveBeenCalled
還是有更多的呢?
請檢查以下示例,它會更改選項卡,加載給定選項卡並隱藏通知;
tabsFn = {
changeTab: function() {
$(tabButtons).addClass('inactive');
$(tabContent).hide();
$(this).removeClass('inactive');
var tab = $(this).attr('tab');
$('.tab-content-' + tab).show();
return false;
},
loadTab: function(tab) {
$(tabButtons).addClass('inactive');
$(tabContent).hide();
$('[tab~="' + tab + '"]').removeClass('inactive').removeAttr('disabled');
$('.tab-content-' + tab).show();
},
messageFn = {
hideNotification: function(time) {
$(messageFn.notificationBar).stop(true, true).fadeOut(time);
},
任何澄清是非常感謝。
優秀答案謝謝,我真的很感謝解釋! – Patrick