标签: httpbackend

意外请求:GET $ httpBackend不再需要更多请求

我的范围内有一个函数可以在用户单击按钮时检索我的服务状态,或者触发某个事件并自动调用此函数.

这是我的功能,在我使用的控制器中定义:

$scope.getStatus = function() {
  $http({method: 'GET', url: config.entrypoint + config.api + '/outbound/service/' + $scope.serviceId})
    .success(function(data) {
      $scope.errorMessage = '';
      $scope.serviceAllGood = data;
    })
    .error(function() {
      $scope.serviceAllGood = '';
      $scope.errorMessage = 'We are experiencing problems retrieving your service status.';
    });
  }
Run Code Online (Sandbox Code Playgroud)

单元测试如下:

describe('SendServiceCtrl', function(){
    var scope, ctrl, $httpBackend, configuration;

    beforeEach(function () {
      module('papi', 'ngUpload');
    });

    beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, config) {
      configuration = config;
      $httpBackend = _$httpBackend_;
      $httpBackend.expectGET(configuration.entrypoint + configuration.api + "/user/outboundstatus/").respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"allowed":false}});
      scope = $rootScope.$new();
      ctrl = $controller('SendServiceCtrl', {$scope: …
Run Code Online (Sandbox Code Playgroud)

jasmine angularjs karma-runner httpbackend

19
推荐指数
1
解决办法
2万
查看次数

如何使$ httpBackend对URL查询参数的顺序不敏感?

我使用Angular.js $httpBackend来测试一些包装$http调用的服务(这是在ngMock中,而不是 ngMockE2E).

看来,喜欢的东西expectwhen对的URL查询参数的顺序是敏感的.例如,如果我这样做,$httpBackend.when('POST','/apiCall?X=1&Y=2').respond(/* ... */)或者如果我在$httpBackend.expectPOST('/apiCall?X=1&Y=2')URL中有Y = 2和X = 1而不是X = 1和Y = 2,则会出现URL不匹配.

我想以这样一种方式编写我的测试,即被测试的服务可以自由地更改URL查询字符串参数的顺序而不会破坏测试.我在$ httpBackend文档中找不到任何解决方法.这样做的正确方法是什么?

angularjs angular-http httpbackend angularjs-http angular-mock

16
推荐指数
2
解决办法
5703
查看次数

如何在AngularJS $ httpBackend ExpectGET中使用正则表达式

我在$ resource GET请求中传递了一个url作为参数.Angular是对此参数进行编码的url,并且在$ httpBackend.expectGET方法中匹配请求是挑剔的.

我看到你可以使用正则表达式来匹配预期的请求,但无法让它工作.我想匹配资源位置,但与任何"uri"查询字符串参数值匹配.

按Ctrl

var resource = $resource('../api/lookup');
resource.get({ uri: "http://www.something.com" }, function (data) {
  // do something
});
Run Code Online (Sandbox Code Playgroud)

测试

// mock out the $httpBackend response

$httpBackend.expectGET(/\.\.\/api\/lookup\?uri=.*/)).respond(200, { Response: "a response" });

// call my test method here

// ensure the $httpBackend work is done
$rootScope.$apply();
$httpBackend.flush();

// do assertions
Run Code Online (Sandbox Code Playgroud)

业力输出

Error: Unexpected request: GET ../api/lookup?uri=http%3A%2F%2Fwww.something.com

Expected GET /../api/lookup?uri=.*/
Run Code Online (Sandbox Code Playgroud)

谁能明白为什么我的正则表达式不匹配?

编辑

即使按照建议改进了正则表达式,我也无法使用' $ var '注入语法来实现这一点.我重写了测试,使用inject函数注入依赖项并确保我的测试安排的顺序是正确的.现在它的工作就像一个魅力!

regex angularjs httpbackend

13
推荐指数
2
解决办法
1万
查看次数

带有查询参数的$ httpBackend请求

$httpBackend.whenGET('/restpath/api/v1/books')
.respond({// some data}); 
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

Error: Unexpected request: GET /restpath/api/v1/books
 Expected GET /restpath/api/v1/books?limit=10&start=1
Run Code Online (Sandbox Code Playgroud)

对于expectGET,我有以下内容,这将创建动态查询字符串.主要是'start'参数和whenGET部分,我试图根据'start'服务器动态内容

$httpBackend.expectGET('/restpath/api/v1/books?limit=10&start=1'); // the actual service goes here , which does the $http service. we don't care $httpBackend.flush();

angularjs httpbackend

11
推荐指数
2
解决办法
1万
查看次数

模块'ngMockE2E'不可用!AngularJS

在我的浏览器中出现以下错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module sayHiApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngMockE2E due to:
Error: [$injector:nomod] Module 'ngMockE2E' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.2.15/$injector/nomod?p0=ngMockE2E
    at http://127.0.0.1:9000/bower_components/angular/angular.js:78:12
    at http://127.0.0.1:9000/bower_components/angular/angular.js:1611:17
    at ensure (http://127.0.0.1:9000/bower_components/angular/angular.js:1535:38)
    at module (http://127.0.0.1:9000/bower_components/angular/angular.js:1609:14)
    at http://127.0.0.1:9000/bower_components/angular/angular.js:3717:22
    at Array.forEach (native)
    at forEach (http://127.0.0.1:9000/bower_components/angular/angular.js:323:11)
    at loadModules (http://127.0.0.1:9000/bower_components/angular/angular.js:3711:5)
    at http://127.0.0.1:9000/bower_components/angular/angular.js:3718:40
    at Array.forEach (native)
http://errors.angularjs.org/1.2.15/$injector/modulerr?p0=ngMockE2E&p1=Error…ngular%2Fangular.js%3A3718%3A40%0A%20%20%20%20at%20Array.forEach%20(native) …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs httpbackend ngmocke2e

10
推荐指数
1
解决办法
4200
查看次数

带有角度模拟的量角器抛出"未定义的窗口"

我在这个问题上搜索了很多,但找不到解决方案.

我正在试图模拟我的后端,经过充分测试,所以我可以完全隔离我的前端.我尝试过使用protractor-http-mock以及角度模拟的各种努力.

使用HttpBackend解决了角度模拟方法后,我在启动量角器测试时遇到了这个错误:

MBP:test-site admin$ protractor protractor.conf.js
Using ChromeDriver directly...
[launcher] Running 1 instances of WebDriver
[launcher] Error: ReferenceError: window is not defined
    at Object.<anonymous> (/Users/Ed/Sites/F4F/web/node_modules/angular/angular.js:30426:4)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/Users/Ed/Sites/F4F/web/node_modules/angular/index.js:1:1)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
[launcher] Process exited with error code 100
Run Code Online (Sandbox Code Playgroud)

这是我的protractor.conf.js

exports.config = {

    directConnect: true,

    // Capabilities to be passed to the webdriver instance.
    capabilities: {
        'browserName': 'chrome'
    }, …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs protractor httpbackend angular-mock

10
推荐指数
1
解决办法
4402
查看次数

我怎么期望不发出HTTP请求?

角的$ httpBackend服务可以让你期望与HTTP请求expectGET,expectPOST等(或只是expect).

我怎么写一个测试说"控制器不应该向这个端点发出请求(在这些条件下)"?

我想的是:

$httpBackend.when('/forbidden/endpoint').respond(function() {
  throw Error("Shouldn't be making a request to /forbidden/endpoint!");
});
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎有些苛刻,但如果这是正常的做事方式,我对它很好.(但我对此表示怀疑.)

angularjs httpbackend

9
推荐指数
2
解决办法
4556
查看次数

尝试使用jasmine和angular时出错

当我尝试使用$ httpBackend.flush(); 我得到错误TypeError:$ browser.cookies不是一个函数.我找不到有关此类错误和任何解决方案的任何信息.

describe("someText", function() {
    var $httpBackend;
    var someManager;
    var authRequestHandler;

    var dataMockup = [];

    beforeEach(function(){
        module('app');

        inject(function($injector){
            $httpBackend = $injector.get('$httpBackend');
            someManager = $injector.get('someManager');

            authRequestHandler = $httpBackend.when('GET', 'someUrl.php')
                .respond(dataMockup);
        });
    });

    it('test first action', function() {
        $httpBackend.expectGET('someUrl.php');
        messageManager.loadData();
        $httpBackend.flush(); // There i got error
    });
});
Run Code Online (Sandbox Code Playgroud)
  • Angular:1.3.15
  • 茉莉花:2.3.4

javascript jasmine angularjs httpbackend

9
推荐指数
1
解决办法
2680
查看次数

如何在AngularJS服务Jasmine测试中模拟$ http?

我正在尝试测试AngularJS服务carService,但$httpBackend似乎不起作用.

//carService
angular.module('services').factory('carService',
    function($http) {
        return {
            getTypes: function() {
                return $http.get('/api/cars/types');
            }
        };
    });
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么响应为空?

describe("Services", function () {

    beforeEach(module("app.services"));

    describe("Car services", function () {

        var service, $httpBackend;

        beforeEach(inject(function($injector) {
            service = $injector.get('carService');
            $httpBackend = $injector.get('$httpBackend');

            $httpBackend.when('GET', "/api/cars/types").respond(["Toyota", "Honda", "Tesla"]);
        }));

        afterEach(function() {
            $httpBackend.verifyNoOutstandingExpectation();
            $httpBackend.verifyNoOutstandingRequest();
        });

        it('getTypes - should return 3 car manufacturers', function () {
            service.getTypes().then(function(response) {
                expect(response.length).toEqual(3); //the response is null
            });
            $httpBackend.flush();
        });


    });
});
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine angularjs httpbackend

9
推荐指数
1
解决办法
9414
查看次数

在AngularJS/karma/jasmine测试中通过$ http测试后端API?

如何使用AngularJS/karma/jasmine测试测试我的API后端?

我试图创建显示我的错误的最小测试用例:

echo_server.py

from bottle import response, route, run

@route('/echo/<echo>')
def echo_echo(echo):
    response.headers['Access-Control-Allow-Origin'] = '*'
    return {'echo': echo}    # Served as JSON

if __name__ == '__main__':
    run()
Run Code Online (Sandbox Code Playgroud)

测试/单元/ apiSpec.js

// Should this be in `test/e2e/apiSpec.js`?
describe('echo', function () {
    var scope, http;

    beforeEach(inject(function ($rootScope, $http) {
        $http.defaults.useXDomain = true;    // CORS
        scope = $rootScope.$new();
        http = $http;
    }));


    it("should echo from server", function () {
        scope.$apply(function () {
            var ret;
            http.get("http://localhost:8080/echo/foo")
                .success(function (data, status, headers, config) {
                             ret = data; …
Run Code Online (Sandbox Code Playgroud)

jasmine angularjs karma-runner httpbackend angularjs-http

8
推荐指数
1
解决办法
1万
查看次数