【发布时间】:2014-11-24 18:49:01
【问题描述】:
我正在尝试测试具有外部模板的 Angular 指令,但无法使其正常工作。这是我第一次尝试使用 Karma。我已经用谷歌搜索了一个解决方案,并尝试了对 karma.conf.js 文件的各种更改,但仍然得到这个:
Error: [$injector:modulerr] Failed to instantiate module app/inventory/itemDetails.html due to:
Error: [$injector:nomod] Module 'app/inventory/itemDetails.html' is not available! You either misspelled the module name or forgot to load it.
文件夹结构:
app
inventory
itemDetails.html
itemDetailsDirective.js (templateUrl: "app/inventory/itemDetails.html")
UnitTest
karma.conf.js
specs
inventorySpec.js
karma.conf.js
// list of files / patterns to load in the browser
files: [
'../scripts/jquery.min.js',
'scripts/angular.js',
'scripts/angular-mocks.js',
'../app/*.js',
'../app/**/*.js',
'../app/inventory/itemDetails.html',
'specs/*.js'
],
preprocessors: {
'../app/inventory/itemDetails.html':['ng-html2js'] // Is this supposed to be the path relative to the karma.conf.js file?
},
ngHtml2JsPreprocessor: {
stripPrefix: '../',
},
itemDetailsDirective.js
templateUrl: "app/inventory/itemDetails.html",
inventorySpec.js(大部分内容出于调试目的而被注释掉)
describe("itemDetailsDirective", function () {
var element, $scope;
beforeEach(module("app/inventory/itemDetails.html"));
beforeEach(inject(function ($compile, $rootScope) {
console.log("itemDetailsDirective");
//element = angular.element('<item-details></item-details>');
//$scope = $rootScope.$new();
//$compile(element)($scope);
//$scope.$digest();
}));
it('should display', function () {
// var isolatedScope = element.isolateScope();
// //expect(isolatedScope.condition).toEqual("Fair");
});
});
所以我有一个与 app 文件夹平行的 UnitTest 文件夹(VS 2013 项目)。 karma.conf.js 中“files”下的路径是正确的 - 所有“non-templateUrl”测试都可以正常工作。
帮助会很棒,虽然我还有一些头发!
【问题讨论】:
-
我认为您可以使用注入服务在您的测试中注入指令,然后从那里获取 templateUrl。我会试着写一个例子。
标签: javascript angularjs unit-testing karma-runner