1
問題:我想訪問我的模板中的表單,並根據驗證,提交等進行測試。我相信我有正確的Karma/Jasmine測試設置,但我不知道如何訪問表單。如何編譯模板HTML,超越.html()?
測試module.view.html:
<div id="main">
<form name="theForm">
<input id="keywords" type="text" ... ></input>
<select id="listOfThings" ... ng-options="...for list..."></select>
<input id="somedate" type="date" ...></input>
</form>
</div>
指令:
(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('<test-directive></test-directive>')($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
})
}