我正在尝试使用Jasmine测试Angular指令。
Html看起来像这样:
<html>
<head>
<title></title>
<script src='/jasmine/jasmine.js'></script>
<script src='/jasmine/jasmine-html.js'></script>
<script src='/jasmine/boot.js'></script>
<script src='/angular/angular.js'></script>
<script src='/angular/angular-mocks.js'></script>
<script src='/components/test-directive.js'></script>
<script src='/app.js'></script>
<script>
angular.module('myAppTests', ['myApp', 'ngMockE2E'])
.run(function($httpBackend){
$httpBackend.whenGET(/^\/views\//).passThrough();
});
</script>
<script src='/components/test-directive.spec.js'></script>
</head>
<body></body>
</html>
测试指令(/components/test-directive.js)
angular.directive('testDirective', function(){
return {
templateUrl:'/views/test-directive.html'
};
});
测试文件(/components/test-directive.spec.js)如下所示:
describe('test directive', function(){
beforeEach(module('myAppTests'));
var element, $scope;
beforeEach(inject(function($compile, $rootScope){
element = angular.element('<div data-test-directive=""></div>');
$scope = $rootScope;
$compile(element)($scope);
$scope.$digest();
}));
it('generates the correct HTML', function(){
expect(element.html()).toContain('test content');
});
});
和/views/test-directive.html
<p>test content</p>
运行测试时出现以下错误:
'Unexpected request: GET /views/test-directive.html'
答案 0 :(得分:1)
尝试将此添加到您的过滤器:
$httpBackend.whenGET(/\.html$/).passThrough();
答案 1 :(得分:0)
在测试中使用任何inject
或module
时,它会隐式加载ngMock的模块,其中包含一个不会发送请求的模拟$ httpBackEnd。
一种选择是重用原始的$ httpBackend。
angular.module('myAppTests', ['myApp', 'ngMock'])
.factory('$httpBackend', function() {
// use the original $httpBackend instead of the fake backend
return angular.injector(['ng']).get('$httpBackend');
});