首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$httpBackend.whenGET() passThrough()未定义

$httpBackend.whenGET() passThrough()未定义
EN

Stack Overflow用户
提问于 2013-08-11 14:34:57
回答 1查看 12.6K关注 0票数 11

我想传递一些http请求,而不是在单元测试中模拟它们,但是当我试图调用passThrough()方法时,会引发丢失方法的错误:

"TypeError: Object #没有方法'passThrough'“。

有人知道我能怎么修吗?

这是我的密码:

代码语言:javascript
复制
'use strict';

describe('Controller: MainCtrl', function () {

    // load the controller's module
    beforeEach(module('w00App'));

    var scope, MainCtrl, $httpBackend;

    // Initialize the controller and a mock scope
    beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
        $httpBackend = _$httpBackend_;
        $httpBackend.expectGET('http://api.some.com/testdata.json').passThrough();


        scope = $rootScope.$new();
        MainCtrl = $controller('MainCtrl', {
            $scope: scope
        });
    }));
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-04 05:24:57

如果您想在开发过程中模拟后端,只需在主html文件中安装angular-mocks,将其添加为应用程序(angular.module('myApp', ['ngMockE2E']))中的依赖项,然后模拟所需的请求。

例如;

代码语言:javascript
复制
angular.module('myApp')
  .controller('MainCtrl', function ($scope, $httpBackend, $http) {
    $httpBackend.whenGET('test').respond(200, {message: "Hello world"});
    $http.get('test').then(function(response){
      $scope.message = response.message //Hello world
    })
  });

但是要小心,添加ngMockE2E将要求您设置路由,以防您通过AngularJS路由设置路由。

榜样;

代码语言:javascript
复制
angular.module('myApp', ['ngMockE2E'])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
  .run(function($httpBackend){
    $httpBackend.whenGET('views/main.html').passThrough();
  })
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18173033

复制
相关文章

相似问题

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