0

我有一个 AngularJS 服务,服务名称是 User。我用它来进行身份验证。在这个服务中,我有一个 authenticate() 函数,它使用 Restangular 对我们的 API 进行 REST 调用。它在我的 Angular 应用程序中完美运行。我刚开始使用业力,所以我可以包括测试。我想知道为什么我会收到这个错误。此外,我阅读了一些 stackoverflow 帖子并注意到他们直接在他们的规范文件中使用 $httpBackend。不幸的是,它与我的设置不同。

INFO [watcher]: Changed file "/Users/admin/repos/acme/acme-ui/test/spec/controllers/main.js".
Chrome 41.0.2272 (Mac OS X 10.8.3) App: CockpitApplication factory: GlobalPermissionsFactory should resolve status to 200 FAILED
    Error: Unexpected request: POST https://someinternaldomain.com/v1/authentokens
    No more request expected
        at $httpBackend (/Users/admin/repos/acme/acme-ui/app/bower_components/angular-mocks/angular-mocks.js:1224:9)
        at sendReq (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:9538:9)
        at serverRequest (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:9255:16)
        at processQueue (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:13075:27)
        at /Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:13091:27
        at Scope.$eval (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:14291:28)
        at Scope.$digest (/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:14107:31)
        at Object.<anonymous> (/Users/admin/repos/acme/acme-ui/test/spec/controllers/main.js:71:24)
Firefox 34.0.0 (Mac OS X 10.8) App: CockpitApplication factory: GlobalPermissionsFactory should resolve status to 200 FAILED
    Error: Unexpected request: POST https://someinternaldomain/v1/authentokens
    No more request expected in /Users/admin/repos/acme/acme-ui/app/bower_components/angular-mocks/angular-mocks.js (line 1224)
    $httpBackend@/Users/admin/repos/acme/acme-ui/app/bower_components/angular-mocks/angular-mocks.js:1224:1
    sendReq@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:9538:1
    $http/serverRequest@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:9255:16
    processQueue@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:13075:27
    scheduleProcessQueue/<@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:13091:27
    $RootScopeProvider/this.$get</Scope.prototype.$eval@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:14291:16
    $RootScopeProvider/this.$get</Scope.prototype.$digest@/Users/admin/repos/acme/acme-ui/app/bower_components/angular/angular.js:14107:15
    @/Users/admin/repos/acme/acme-ui/test/spec/controllers/main.js:71:13
Chrome 41.0.2272 (Mac OS X 10.8.3): Executed 4 of 4 (1 FAILED) (0.114 secs / 0.111 secs)
Firefox 34.0.0 (Mac OS X 10.8): Executed 4 of 4 (1 FAILED) (0.101 secs / 0.092 secs)
TOTAL: 2 FAILED, 6 SUCCESS

这是我的 main.js,Karma 规范文件

describe('App: CockpitApplication', function () {

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

    var $controller, $q, $rootScope;
    beforeEach(inject(function(_$controller_, _$q_, _$rootScope_) {
        $controller = _$controller_;
        $q = _$q_;
        $rootScope = _$rootScope_;
    }));

    describe('factory: GlobalPermissionsFactory', function() {
        var _GlobalPermissionsFactory = null,
            _UserInformationFactory = null,
            _User = null;

        beforeEach(inject(function(GlobalPermissionsFactory) {
            _GlobalPermissionsFactory = GlobalPermissionsFactory;
        }));

        beforeEach(inject(function(UserInformationFactory) {
            _UserInformationFactory = UserInformationFactory;
        }));

        beforeEach(inject(function(User) {
            _User = User;
        }));

        it('should return false', function() {
            expect(_GlobalPermissionsFactory).toBeDefined()
        });

        it('should check if getUserInformation() exists', function() {
            expect(_UserInformationFactory.getUserInformation).toBeDefined()
        });

        it('should return a promise', function() {
            expect(_User.authenticate('admin', 'secret').then).toBeDefined();
        });

        it('should resolve status to 200', function () {
            var data;

            // set up a deferred
            var deferred = $q.defer();
            // get promise reference
            var promise = deferred.promise;

            // set up promise resolve callback
            promise.then(function (response) {
                console.log('data',data);
                data = response.status;
            });

            _User.authenticate('admin', 'secret').then(function(response) {
                // resolve our deferred with the response when it returns
                deferred.resolve(response);
            });

            // force `$digest` to resolve/reject deferreds
            $rootScope.$digest();

            // make your actual test
            expect(data).toEqual(200);

        });
    });
});
4

1 回答 1

1

您确实需要使用 $httpBackend - 像下面这样使用它应该适合您。

文档中:

在单元测试期间,我们希望我们的单元测试能够快速运行并且没有外部依赖,因此我们不希望将 XHR 或 JSONP 请求发送到真实服务器。我们真正需要的是验证某个请求是否已发送,或者只是让应用程序发出请求,用预先训练的响应进行响应,并断言最终结果是我们期望的。

使用 $httpBackend 可以让您单独测试服务的方法 - 测试的成功不取决于服务器必须正确执行任何操作。您还可以测试该方法在其请求中发送的内容。

如果不使用 $httpBackend,您实际上是在进行服务器调用,这可能会导致各种错误。一个猜测,但我会说你得到的错误就是其中之一。

describe('App: CockpitApplication', function () {

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

    var $controller, $q, $rootScope, $httpBackend;
    beforeEach(inject(function(_$controller_, _$q_, _$rootScope_, _$httpBackend_) {
        $controller = _$controller_;
        $q = _$q_;
        $rootScope = _$rootScope_;
        $httpBackend = _$httpBackend_;
    }));

    describe('factory: GlobalPermissionsFactory', function() {
        var _GlobalPermissionsFactory = null,
            _UserInformationFactory = null,
            _User = null;

        beforeEach(inject(function(GlobalPermissionsFactory) {
            _GlobalPermissionsFactory = GlobalPermissionsFactory;
        }));

        beforeEach(inject(function(UserInformationFactory) {
            _UserInformationFactory = UserInformationFactory;
        }));

        beforeEach(inject(function(User) {
            _User = User;
        }));

        it('should return false', function() {
            expect(_GlobalPermissionsFactory).toBeDefined()
        });

        it('should check if getUserInformation() exists', function() {
            expect(_UserInformationFactory.getUserInformation).toBeDefined()
        });

        it('should return a promise', function() {
            expect(_User.authenticate('admin', 'secret').then).toBeDefined();     
        });

        it('should resolve status to 200', function () {
            var data;
          
            $httpBackend.whenGET('//url here').respond(200, //return something the server would);
                                                       
            // set up a deferred
            var deferred = $q.defer();
            // get promise reference
            var promise = deferred.promise;

            // set up promise resolve callback
            promise.then(function (response) {
                console.log('data',data);
                data = response.status;
            });

            _User.authenticate('admin', 'secret').then(function(response) {
                // resolve our deferred with the response when it returns
                deferred.resolve(response);
            });
            $httpBackend.flush();
            // force `$digest` to resolve/reject deferreds
            $rootScope.$digest();

            // make your actual test
            expect(data).toEqual(200);

        });
    });
});

于 2015-03-04T04:10:06.433 回答