问题:我想访问我的模板中的表单,并根据验证,提交等进行测试。我相信我有正确的Karma/Jasmine测试设置,但我不知道如何访问表单。如何编译模板HTML,超越.html()?
测试module.view.html:
指令:
(function(){
"use strict";
angular.module('TestModule').directive('testDirective',Directive);
function Directive(){
return {
restrict: 'E',
controller: 'TestModuleController',
controllerAs: 'testModuleVm',
templateUrl: 'testModule/test-module.view.html'
};
};
})();
测试:
describe('Directive Tests',function(){
var element,$compile,$rootScope;
beforeEach(module('myTemplates'));
beforeEach(inject([
'$compile','$rootScope',
function($c,$rs){
$compile = $c;
$rootScope = $rs;
}
]));
it('should test an input element',function(){
element = $compile('')($rootScope);
$rootScope.$digest();
console.log(element); // Should see your template displayed
});
});
个karma.conf.js:
// Created May 04, 2016
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files/patterns to load in the browser
files: [
'vendor/angular/angular.js',
'vendor/angular/angular-mocks.js',
'test-module/test-module.view.html',
'test-module/test-module.module.js',
'test-module/test-module.controller.js',
'test-module/test-module.directive.js',
'test/test-module-tests.js',
],
// list of files to exclude
exclude: [
'vendor/bootstrap*',
'vendor/jquery*',
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma- preprocessor
preprocessors: {
// karma-ng-html2js-preprocessor for templates in directives
'testModule/test-module.view.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
moduleName: 'myTemplates'
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable/disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable/disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
2016-05-17
westandy