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 
    }) 
} 

回答

2

了很多学习和挫折后,对于那些你噶从头开始了像我一样,没有本地支持的,随意使用其作为模板。有很多方法来设置指令测试,但我终于有了这种方式来工作,我可以阅读它。

第一个,可悲的是,我在karma.conf.js中丢失了我的指令文件。

,预处理(在karma.conf预处理器,ngHtml2JsPreprocessor在karma.conf,和业力-NG-html2js预处理器NPM模块)你的HTML模板,并将其存储到一个模块将帮助您试验。就我而言,我有大约20个模块,每个模块都有一个模板/指令。因此,将它们放入一个大模板模块并加载它用于此类型的每个测试都很方便。

终于,我仍然不知道如何对表单验证进行测试,这引发了这个问题,但我更接近完成这项工作。