在文件dashboard.module.coffee
中,我有以下声明:
angular
.module('app.dashboard', [])
在另一个文件stat.directive.coffee
中,我有以下内容:
angular.module('app.dashboard')
.directive('stat', ['$interval', statDirective])
statDirective
包含指令逻辑。此代码在浏览器中工作正常,即<stat>
元素按预期工作,但以下Jasmine测试不呈现元素,它只是一个空字符串:
describe "Stat", ->
element = null
scope = null
beforeEach module 'app.dashboard'
beforeEach module 'views/templates/dashboard/stat.html'
beforeEach inject ($compile, $rootScope) ->
scope = $rootScope.$new()
element = $compile('<stat></stat>') scope
it "contains some html", ->
scope.$digest()
expect(element.html()).toEqual('<div>hi</div>')
我已将其缩小到与指令分开声明的模块。如果相反,声明是这样的,找到并呈现指令:
angular.module('app.dashboard', [])
.directive('stat', ['$interval', statDirective])
这里,唯一的变化是模块和指令被一起声明,而不是两个文件。
这看起来是Karma特有的问题,因为代码在浏览器中运行良好。我的Karma配置中是否缺少这种类型的文件结构?
这是我的Karma配置:
// Karma configuration
// Generated on Mon Jun 29 2015 22:33:24 GMT-0700 (PDT)
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: [
'client/bower_components/jquery/dist/jquery.min.js',
{pattern: 'client/bower_components/**/*.map', watched: false, included: false, served: true},
'client/bower_components/angular/angular.min.js',
'client/bower_components/angular-websocket/angular-websocket.js',
'node_modules/angular-mocks/angular-mocks.js',
'client/views/**/*.html',
'client/scripts/*.module.js',
'client/scripts/**/*.module.js',
'client/scripts/**/*.module.coffee',
'client/scripts/**/*.directive.coffee',
'client/scripts/**/*.controller.coffee',
'client/scripts/**/*.coffee',
'spec/**/*Spec.coffee'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'**/*.coffee': ['coffee'],
'client/views/**/*.html': 'ng-html2js'
},
ngHtml2JsPreprocessor: {
// strip app from the file path
stripPrefix: 'client/'
},
// 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: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
})
};
谢谢!
答案 0 :(得分:1)
也许我们必须采取步骤......
我发现您的指令 stat 具有依赖关系:$ interval。
如果我记得,您可能需要包含此服务以使其正常运行。
你能试试吗?
beforeEach inject ($compile, $rootScope, _$interval_) ->
scope = $rootScope.$new()
element = $compile('<stat></stat>') scope
$interval = _$interval_; // you may not need this line, just the inject.
<强>更新强>
好的,试试这个
更改此
element = $compile('<stat></stat>') scope
到此
element = $compile( angular.element('<stat></stat>'))(scope);