我正在尝试使用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>测试指令(/component/ test -directive.js)
angular.directive('testDirective', function(){
return {
templateUrl:'/views/test-directive.html'
};
});测试文件(/component/ 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'发布于 2015-06-12 00:29:37
尝试将此内容添加到您的过滤器中:
$httpBackend.whenGET(/\.html$/).passThrough();发布于 2016-04-30 15:38:28
当您在测试中使用任何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');
});https://stackoverflow.com/questions/30786238
复制相似问题