私はシステムライフサイクルの逆の作業をしています。数か月前、私は大規模な JavaScript ライブラリを作成しました。その後、すべてを客観的にする必要がありましたが、今では単体テストを作成する必要があります。私はMavenを使用しておりjasmine-maven-plugin
、pom.xmlに. 私が抱えている問題は、何のためにテストを書くべきか、そしていくつテストを書くべきかということです。
この最初の例は単純です。この関数は文字列を受け取り、最初の文字を大文字にして返します。
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);
},
説明をいただければ幸いです。