Sau khi chi tiêu ngày cuối cùng cố gắng thực hiện công việc này, tôi thấy rằng tôi đã quay trở lại cùng một lỗi tôi đã gặp lúc đầu :Karma 'Yêu cầu bất ngờ' khi kiểm tra chỉ thị góc, ngay cả với ng-html2js
Lỗi: yêu cầu mong đợi: GET thử directive.html
tôi đang sử dụng Karma và Jasmine để kiểm tra các chỉ thị trong góc. Tôi đã xem xét các câu hỏi tương tự trên StackOverflow, nhưng thấy rằng mọi thứ đã được thử trong các ví dụ khác là không có kết quả.
cấu trúc Mã
Test-App
-src
--bower
--lib
--js
--modules
--- testDir
--- -test.js
---- test-directive.html
---- test
----- test.spec.js
-test
--config
--- karma.conf.js
--e2e
Karma Config
'use strict';
module.exports = function(config){
config.set({
basePath: '../../',
frameworks: ['jasmine'],
files: [
// Angular
'src/bower/angular/angular.js',
// Mocks
'src/bower/angular-mocks/angular-mocks.js',
// Libraries
'src/lib/**/*.js',
// App
'src/js/*.js',
'src/modules/*/*.js',
// Tests
'src/modules/**/test/*spec.js',
// Templates
'src/modules/**/*.html'
],
autoWatch: false,
singleRun: true,
reporters: ['progress'],
browsers: ['PhantomJS'],
preprocessors: {
'src/modules/**/*.html': 'ng-html2js'
},
ngHtml2JsPreprocessor: {
moduleName: 'dir-templates'
},
plugins: [
'karma-jasmine',
'karma-ng-html2js-preprocessor',
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-junit-reporter'
]
});
};
test.js
'use strict';
angular.module('modules.test', []).
directive('testDirective', [function() {
return {
restrict: 'E',
templateUrl: 'test-directive.html',
link: function($scope, $elem, $attrs) {
$scope.someFn = function() {
angular.noop();
};
}
};
}]);
thử direct.html
<span>Hello World</span>
test.spec.js
'use strict';
describe('test module', function() {
beforeEach(module('modules.test'));
/* -- DIRECTIVES------------------ */
describe('directives', function() {
var $compile, $scope, elm;
beforeEach(module('dir-templates');
beforeEach(inject(function($compile, $rootScope) {
$scope = $rootScope.$new();
elm = angular.element('<test-directive></test-directive>');
$compile(elm)($scope);
$scope.$digest();
}));
it('should have one span tag', function(){
//Jasmine test here to check for one span tag.
});
});
});
đã rút ngắn một vài tập tin để dính vào chỉ nơi vấn đề là. Khi gọi số beforeEach(module('dir-templates'))
, nó sẽ tải tất cả các tệp .html đã khớp vào $ templateCache và ngăn yêu cầu GET đang gửi lỗi.
Bất kỳ trợ giúp nào sẽ được đánh giá cao vì nó thực sự khiến tôi phát điên. Vui lòng nhận xét nếu bạn có thêm bất kỳ câu hỏi nào.
tôi đã sử dụng lời khuyên của bạn. Đây là một pic của những gì tôi đã làm: http://i.stack.imgur.com/h5y8a.png; Sau đó, tôi thấy rằng một dấu gạch chéo bị thiếu trong tiền tố của tôi. –
Cảm ơn vì điều này, ngoài việc giúp tôi xác định vấn đề của mình, bạn đã truyền cảm hứng cho tôi không chỉ phụ thuộc vào PhantomJS và sử dụng các trình duyệt khác. –
Bạn có thể xem tại đây http://stackoverflow.com/questions/42366791/angular-js-load-json-file-directive-test-failing-mocha – Manwal