我在一个由 yeoman gulp-angular 生成器引导的项目中使用 gulp(不是 grunt)和 Jade 模板。为了使 Jasmine 单元测试正常工作,我需要进行以下更改:
在 gulp/unit-tests.js 中:
var htmlFiles = [
- options.src + '/**/*.html'
+ options.src + '/**/*.html',
+ options.tmp + '/serve/**/*.html' // jade files are converted to HTML and dropped here
];
...
- gulp.task('test', ['scripts'], function(done) {
+ gulp.task('test', ['markups','scripts'], function(done) {
runTests(true, done);
});
在 karma.conf.js 中:
ngHtml2JsPreprocessor: {
- stripPrefix: 'src/',
- moduleName: 'gulpAngular'
+ cacheIdFromPath: function(filepath) {
+ // jade files come from .tmp/serve so we need to strip that prefix
+ return filepath.substr(".tmp/serve/".length);
+ },
+ moduleName: 'myAppTemplates'
},
...
preprocessors: {
- 'src/**/*.html': ['ng-html2js']
+ // jade templates are converted to HTML and dropped here
+ '.tmp/serve/**/*.html': ['ng-html2js']
}
这是页脚指令的单元测试文件:
'use strict';
describe('Unit testing footer', function() {
var $compile,
$rootScope;
// Load the myApp module, which contains the directive
beforeEach(module('myApp'));
beforeEach(module('myAppTemplates')); // generated in karma.conf
// Store references to $rootScope and $compile
// so they are available to all tests in this describe block
beforeEach(inject(function(_$compile_, _$rootScope_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('Replaces the element with the appropriate content', function() {
// Compile a piece of HTML containing the directive
var element = $compile('<footer/>')($rootScope);
// fire all the watches, so the scope expression {{1 + 1}} will be evaluated
$rootScope.$digest();
// Check that the compiled element contains the templated content
expect(element.html()).toContain('Copyright');
});
});