Tôi đang sử dụng Karma và Jasmine để kiểm tra đơn vị cho ứng dụng angularjs của tôi. Tôi có một chỉ thị (nói Chỉ thị A) mẫu trong đó một chỉ thị khác (nói Chỉ thị B) được đưa ra, mặc dù nó hoạt động tốt trong ứng dụng nhưng trường hợp thử nghiệm không hiển thị mẫu của Chỉ thị B. Tiếp theo là lỗi tôi nhận được: -Lỗi: Yêu cầu không mong muốn: GET views/partials/* cho chỉ thị lồng nhau ngay cả khi sử dụng html2js trong thử nghiệm đơn vị nghiệp/hoa nhài
Error: Unexpected request: GET views/partials/directiveb.html
Expected GET https://my-sandbox.app.com/123456
Dưới đây là chỉ thị của một mã: -
angular.module('myApp')
.directive('directiveA', function (myservices, myOtherServices) {
return {
controller: function(){
/* ... controller function ... */
},
templateUrl: '/views/partials/directivea.html',
restrict: 'E',
link: function postLink(scope, element, attrs) {
/* ...link function... */
}
};
});
Chỉ thị của một mẫu: -
<div>
<div class="col-md-12">
<h4>We <strong>Need</strong></h4>
<div directive-b some-attribute=="true"></div>
</div>
<div directive-b some-attribute=="false"></div>
</div>
test Chỉ thị của một: -
'use strict';
describe('Directive: directiveA', function() {
// load the directive's module
beforeEach(module('myApp'));
beforeEach(module('template-module'));
var element, appId, reqResponse, scope, dscope, reqUrl, $httpBackend, $compile;
beforeEach(inject(function ($rootScope, _$httpBackend_) {
scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
appId = "123456";
reqUrl = "https://my-sandbox.app.com/" + appId;
reqResponse = {}
}));
it('should Pass', inject(function (_$compile_) {
$httpBackend.expect('GET', reqUrl).respond(reqResponse);
$compile = _$compile_;
element = angular.element('<directive-a/>');
element = $compile(element)(scope);
scope.$digest();
$httpBackend.flush();
dscope = element.scope();
expect(dscope.myVar).toBe(true);
}));
});
Tệp cấu hình Karma: -
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// generator-karma 0.8.2
module.exports = function(config) {
config.set({
autoWatch: true,
basePath: '../',
frameworks: ['jasmine'],
preprocessors: {
'app/views/**/*.html': 'html2js'
},
ngHtml2JsPreprocessor: {
stripPrefix: "app",
moduleName: "template-module"
},
// list of files/patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'bower_components/angular-strap/dist/angular-strap.min.js',
'bower_components/angular-strap/dist/angular-strap.tpl.min.js',
'bower_components/ng-file-upload/angular-file-upload-shim.min.js',
'bower_components/ng-file-upload/angular-file-upload.js',
'bower_components/jquery/dist/jquery.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js',
'app/views/**/*.html'
],
// list of files/patterns to exclude
exclude: ['test/spec/e2e/*'],
// web server port
port: 8080,
browsers: ['PhantomJS'],
// Which plugins to enable
plugins: [
// 'karma-chrome-launcher',
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor'
],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO
// Uncomment the following lines if you are using grunt's server to run the tests
//proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
LƯU Ý: Tôi đã sử dụng html2js cho $ templateCache và tôi vẫn gặp sự cố này.
Wow, thật là một sự giám sát! Tôi đã tự hỏi tại sao chỉ có một trong những chỉ thị của tôi đã gây ra điều này, như tất cả mọi thứ trong tiền xử lý của tôi đã được cấu hình hoàn hảo trong nhiều tháng. Sau đó, chỉ cần một lỗi sao chép/dán. – SoEzPz
Tôi đã phạm sai lầm tương tự. Cảm ơn bạn, bạn đã cứu tôi! – nikjohn