目录搜索
AngularJS API Referenceautoauto/serviceauto/service/$injectorauto/service/$providengng/directiveng/directive/ang/directive/formng/directive/inputng/directive/input[checkbox]ng/directive/input[date]ng/directive/input[dateTimeLocal]ng/directive/input[email]ng/directive/input[month]ng/directive/input[number]ng/directive/input[radio]ng/directive/input[text]ng/directive/input[time]ng/directive/input[url]ng/directive/input[week]ng/directive/ngAppng/directive/ngBindng/directive/ngBindHtmlng/directive/ngBindTemplateng/directive/ngBlurng/directive/ngChangeng/directive/ngCheckedng/directive/ngClassng/directive/ngClassEvenng/directive/ngClassOddng/directive/ngClickng/directive/ngCloakng/directive/ngControllerng/directive/ngCopyng/directive/ngCspng/directive/ngCutng/directive/ngDblclickng/directive/ngDisabledng/directive/ngFocusng/directive/ngFormng/directive/ngHideng/directive/ngHrefng/directive/ngIfng/directive/ngIncludeng/directive/ngInitng/directive/ngKeydownng/directive/ngKeypressng/directive/ngKeyupng/directive/ngListng/directive/ngModelng/directive/ngModelOptionsng/directive/ngMousedownng/directive/ngMouseenterng/directive/ngMouseleaveng/directive/ngMousemoveng/directive/ngMouseoverng/directive/ngMouseupng/directive/ngNonBindableng/directive/ngOpenng/directive/ngPasteng/directive/ngPluralizeng/directive/ngReadonlyng/directive/ngRepeatng/directive/ngSelectedng/directive/ngShowng/directive/ngSrcng/directive/ngSrcsetng/directive/ngStyleng/directive/ngSubmitng/directive/ngSwitchng/directive/ngTranscludeng/directive/ngValueng/directive/scriptng/directive/selectng/directive/textareang/filterng/filter/currencyng/filter/dateng/filter/filterng/filter/jsonng/filter/limitTong/filter/lowercaseng/filter/numberng/filter/orderByng/filter/uppercaseng/functionng/function/angular.bindng/function/angular.bootstrapng/function/angular.copyng/function/angular.elementng/function/angular.equalsng/function/angular.extendng/function/angular.forEachng/function/angular.fromJsonng/function/angular.identityng/function/angular.injectorng/function/angular.isArrayng/function/angular.isDateng/function/angular.isDefinedng/function/angular.isElementng/function/angular.isFunctionng/function/angular.isNumberng/function/angular.isObjectng/function/angular.isStringng/function/angular.isUndefinedng/function/angular.lowercaseng/function/angular.moduleng/function/angular.noopng/function/angular.toJsonng/function/angular.uppercaseng/objectng/object/angular.versionng/providerng/provider/$animateProviderng/provider/$compileProviderng/provider/$controllerProviderng/provider/$filterProviderng/provider/$httpProviderng/provider/$interpolateProviderng/provider/$locationProviderng/provider/$logProviderng/provider/$parseProviderng/provider/$rootScopeProviderng/provider/$sceDelegateProviderng/provider/$sceProviderng/serviceng/service/$anchorScrollng/service/$animateng/service/$cacheFactoryng/service/$compileng/service/$controllerng/service/$documentng/service/$exceptionHandlerng/service/$filterng/service/$httpng/service/$httpBackendng/service/$interpolateng/service/$intervalng/service/$localeng/service/$locationng/service/$logng/service/$parseng/service/$qng/service/$rootElementng/service/$rootScopeng/service/$sceng/service/$sceDelegateng/service/$templateCacheng/service/$timeoutng/service/$windowng/typeng/type/$cacheFactory.Cacheng/type/$compile.directive.Attributesng/type/$rootScope.Scopeng/type/angular.Moduleng/type/form.FormControllerng/type/ngModel.NgModelControllerngAnimatengAnimate/providerngAnimate/provider/$animateProviderngAnimate/servicengAnimate/service/$animatengCookiesngCookies/servicengCookies/service/$cookiesngCookies/service/$cookieStorengMessagesngMessages/directivengMessages/directive/ngMessagengMessages/directive/ngMessagesngMockngMock/functionngMock/function/angular.mock.dumpngMock/function/angular.mock.injectngMock/function/angular.mock.modulengMock/objectngMock/object/angular.mockngMock/providerngMock/provider/$exceptionHandlerProviderngMock/servicengMock/service/$exceptionHandlerngMock/service/$httpBackendngMock/service/$intervalngMock/service/$logngMock/service/$timeoutngMock/typengMock/type/angular.mock.TzDatengMockE2EngMockE2E/servicengMockE2E/service/$httpBackendngResourcengResource/servicengResource/service/$resourcengRoutengRoute/directivengRoute/directive/ngViewngRoute/providerngRoute/provider/$routeProviderngRoute/servicengRoute/service/$routengRoute/service/$routeParamsngSanitizengSanitize/filterngSanitize/filter/linkyngSanitize/servicengSanitize/service/$sanitizengTouchngTouch/directivengTouch/directive/ngClickngTouch/directive/ngSwipeLeftngTouch/directive/ngSwipeRightngTouch/servicengTouch/service/$swipe
文字

AngularJS: API: ngMockE2E/service/$httpBackend


$httpBackend

  1. - service in module ngMockE2E

Fake HTTP backend implementation suitable for end-to-end testing or backend-less development of applications that use the $http service.

注意: For fake http backend implementation suitable for unit testing please see unit-testing $httpBackend mock.

This implementation can be used to respond with static or dynamic responses via the when api and its shortcuts (whenGET, whenPOST, etc) and optionally pass through requests to the real $httpBackend for specific requests (如: to interact with certain remote apis or to fetch templates from a webserver).

As opposed to unit-testing, in an end-to-end testing scenario or in scenario when an application is being developed with the real backend api replaced with a mock, it is often desirable for certain category of requests to bypass the mock and issue a real http request (如: to fetch templates or static files from the webserver). To configure the backend with this behavior use the passThrough request handler of when instead of respond.

此外, we don't want to manually have to flush mocked out requests like we do during unit testing. For this reason the e2e $httpBackend automatically flushes mocked out requests automatically, closely simulating the behavior of the XMLHttpRequest object.

To setup the application to run with this http backend, you have to create a module that depends on the ngMockE2E and your application modules and defines the fake backend:

  myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
  myAppDev.run(Function($httpBackend) {
    phones = [{name: 'phone1'}, {name: 'phone2'}];

    // returns the current list of phones
    $httpBackend.whenGET('/phones').respond(phones);

    // adds a new phone to the phones array
    $httpBackend.whenPOST('/phones').respond(Function(method, url, data) {
      var phone = angular.fromJson(data);
      phones.push(phone);
      return [200, phone, {}];
    });
    $httpBackend.whenGET(/^\/templates\//).passThrough();
    //...
  });

Afterwards, bootstrap your app with this new module.

方法

  • when(method, url, [data], [headers]);

    Creates a new backend definition.

    参数

    参数 类型 详述
    method string

    HTTP method.

    url stringRegExpfunction(string)

    HTTP url or function that receives the url and returns true if the url match the current definition.

    data
    (可选)
    stringRegExp

    HTTP request body.

    headers
    (可选)
    Objectfunction(Object)

    HTTP headers or function that receives http header object and returns true if the headers match the current definition.

    返回值

    requestHandler

    Returns an object with respond and passThrough methods that control how a matched request is handled.

    • respond – {Function([status,] data[, headers, statusText]) | Function(Function(method, url, data, headers)} – The respond method takes a set of static data to be returned or a function that can return an array containing response status (number), response data (string), response headers (Object), and the text for the status (string).
    • passThrough – {Function()} – Any request matching a backend definition with passThrough handler will be passed through to the real backend (an XHR request will be made to the server.)
  • whenGET(url, [headers]);

    Creates a new backend definition for GET requests. For more info see when().

    参数

    参数 类型 详述
    url stringRegExpfunction(string)

    HTTP url or function that receives the url and returns true if the url match the current definition.

    headers
    (可选)
    Objectfunction(Object)

    HTTP headers.

    返回值

    requestHandler

    Returns an object with respond and passThrough methods that control how a matched request is handled.

  • whenHEAD(url, [headers]);

    Creates a new backend definition for HEAD requests. For more info see when().

    参数

    参数 类型 详述
    url stringRegExpfunction(string)

    HTTP url or function that receives the url and returns true if the url match the current definition.

    headers
    (可选)
    Objectfunction(Object)

    HTTP headers.

    返回值

    requestHandler

    Returns an object with respond and passThrough methods that control how a matched request is handled.

  • whenDELETE(url, [headers]);

    Creates a new backend definition for DELETE requests. For more info see when().

    参数

    参数 类型 详述
    url stringRegExpfunction(string)

    HTTP url or function that receives the url and returns true if the url match the current definition.

    headers
    (可选)
    Objectfunction(Object)

    HTTP headers.

    返回值

    requestHandler

    Returns an object with respond and passThrough methods that control how a matched request is handled.

  • whenPOST(url, [data], [headers]);

    Creates a new backend definition for POST requests. For more info see when().

    参数

    参数 类型 详述
    url stringRegExpfunction(string)

    HTTP url or function that receives the url and returns true if the url match the current definition.

    data
    (可选)
    stringRegExp

    HTTP request body.

    headers
    (可选)
    Objectfunction(Object)

    HTTP headers.

    返回值

    requestHandler

    Returns an object with respond and passThrough methods that control how a matched request is handled.

  • whenPUT(url, [data], [headers]);

    Creates a new backend definition for PUT requests. For more info see when().

    参数

    参数 类型 详述
    url stringRegExpfunction(string)

    HTTP url or function that receives the url and returns true if the url match the current definition.

    data
    (可选)
    stringRegExp

    HTTP request body.

    headers
    (可选)
    Objectfunction(Object)

    HTTP headers.

    返回值

    requestHandler

    Returns an object with respond and passThrough methods that control how a matched request is handled.

  • whenPATCH(url, [data], [headers]);

    Creates a new backend definition for PATCH requests. For more info see when().

    参数

    参数 类型 详述
    url stringRegExpfunction(string)

    HTTP url or function that receives the url and returns true if the url match the current definition.

    data
    (可选)
    stringRegExp

    HTTP request body.

    headers
    (可选)
    Objectfunction(Object)

    HTTP headers.

    返回值

    requestHandler

    Returns an object with respond and passThrough methods that control how a matched request is handled.

  • whenJSONP(url);

    Creates a new backend definition for JSONP requests. For more info see when().

    参数

    参数 类型 详述
    url stringRegExpfunction(string)

    HTTP url or function that receives the url and returns true if the url match the current definition.

    返回值

    requestHandler

    Returns an object with respond and passThrough methods that control how a matched request is handled.


上一篇:下一篇: