Mic*_*len 5 javascript unit-testing angularjs karma-jasmine webpack
前提条件:我正在使用Karma对我的Angular.js应用程序模块运行Jasmine单元测试.
我的应用程序使用以下模式来公开模块(服务/指令/控制器):
simple.js
'use strict';
export default (angular) => {
angular.module('simple', [])
.directive('simple', [function() {
return {
restrict: 'E',
replace: true,
template: '<h1>COMPILED!</h1>'
};
}]);
};
Run Code Online (Sandbox Code Playgroud)
上面示例的相应单元测试如下所示:
simple.test.js
import angular from 'angular';
import mocks from 'angular-mocks';
import simpleModule from './simple';
describe('simple', () => {
var $compile,
$rootScope;
// Load the simple module, which contains the directive
beforeEach(() => {
let simpleComponent = new simpleModule(angular);
// module('simple') raises a TypeError here
// I have also tried angular.module('simple') which
// I am pretty sure is incorrect.
});
// Store references to $rootScope and $compile
// so they are available to all tests in this describe block
beforeEach(inject((_$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', () => {
// Compile a piece of HTML containing the directive
var element = angular.element("<simple>not compiled</simple>");
var compiledElement = $compile(element)($rootScope);
// fire all the watches, so the scope expressions evaluate
$rootScope.$digest();
// Check that the compiled element contains the templated content
expect(element.html()).toContain("COMPILED!");
});
});
Run Code Online (Sandbox Code Playgroud)
问题:在Web浏览器中使用Karma运行上述测试时,测试失败,并且元素似乎没有被编译.
我错过了什么?
小智 0
我怀疑您没有正确导入指令。你有没有尝试过:
beforeEach(module('simple'));
Run Code Online (Sandbox Code Playgroud)
您指出您尝试过的替代版本不正确或者是我没有见过的模式:
beforeEach(() => {
let simpleComponent = new simpleModule(angular);
});
module('simple');
angular.module('simple');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1500 次 |
| 最近记录: |