私は Angular JS プロジェクトの最初の単体テストを書いていますが、理解しやすく維持しやすい方法でレイアウトする方法を考えています。
たとえば、ディレクティブの場合、これまでに見つけた例には通常、ディレクティブ用の 1 つのファイルがあり、その中に 1 つの describe() が含まれています。私の意見では、それは維持するのに非常に大きなファイルになりやすいと思います。
ディレクティブごとにファイルを作成し、そのディレクティブでディレクティブ自体の説明を作成する方が理にかなっていると思います。これは、myDate ディレクティブの場合のように、ファイル名が「myDateDirectiveSpec.js」です。これですでに読みやすくなっていますが、多くの関数を含むディレクティブについてはまだ少し心配です。以下の例では、どこでどの関数をテストしたかを示すコメントを追加していますが、もっと良い方法はありませんか?
describe("myDate", function() {
var $compile, $rootScope;
var validDate, invalidDate, invalidDateFormat;
beforeEach(angular.mock.module('main'));
beforeEach(inject(
['$compile','$rootScope', function($c, $r) {
$compile = $c;
$rootScope = $r;
}]
));
// test function validDate
it("should check if the given date is a valid date", function() {
validDate = '31-8-2011';
expect(isValidDate(validDate)).toBe(true);
})
// test function formatDate
it("format the given date", function() {
validDate = '31-8-2011';
expect(formatDate(validDate)).toBe('31/8/2011');
})
// test function anotherFunction
it("....", function() {
validDate = '31-8-2011';
expect(anotherFunction(validDate)).toBe(true);
})
// test function anotherFunction
it("....", function() {
validDate = '31-8-2011';
expect(anotherFunction(validDate)).toBe(true);
})
// test function anotherFunction
it("....", function() {
validDate = '31-8-2011';
expect(anotherFunction(validDate)).toBe(true);
})
});
ディレクティブに多くの関数が含まれている場合、上記の例で行ったことよりもスマートな方法でそれらを分割できますか?