首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular:意外请求(ngMockE2E - $httpBackend)

Angular:意外请求(ngMockE2E - $httpBackend)
EN

Stack Overflow用户
提问于 2015-06-12 00:21:11
回答 2查看 419关注 0票数 0

我正在尝试使用Jasmine测试一个Angular指令。

Html看起来像这样:

代码语言:javascript
复制
<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)

代码语言:javascript
复制
angular.directive('testDirective', function(){
    return {
        templateUrl:'/views/test-directive.html'
    };
});

测试文件(/component/ test -directive.spec.js)如下所示:

代码语言:javascript
复制
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

代码语言:javascript
复制
<p>test content</p>

在运行测试时,我得到以下错误:

代码语言:javascript
复制
'Unexpected request: GET /views/test-directive.html'
EN

回答 2

Stack Overflow用户

发布于 2015-06-12 00:29:37

尝试将此内容添加到您的过滤器中:

代码语言:javascript
复制
$httpBackend.whenGET(/\.html$/).passThrough();
票数 1
EN

Stack Overflow用户

发布于 2016-04-30 15:38:28

当您在测试中使用任何injectmodule时,它会隐式加载ngMock的模块,其中包括一个不发送请求的模拟$httpBackEnd。

一种选择是重用原始的$httpBackend。

代码语言:javascript
复制
angular.module('myAppTests', ['myApp', 'ngMock'])
  .factory('$httpBackend', function() {
    // use the original $httpBackend instead of the fake backend
    return angular.injector(['ng']).get('$httpBackend');
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30786238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档