2015-06-30 35 views
1

我試圖單元測試一個使用$http服務發出http請求的方法,但$httpbackend模擬似乎不會攔截請求 - 我得到一個Error: No pending request to flush !錯誤。

我的測試如下:

/// <reference path="../typings/tsd.d.ts" /> 
/// <reference path="../dist/ngJwtAuth.d.ts" /> 


var expect = chai.expect; 

describe('Service tests',() => { 

    var $httpBackend:ng.IHttpBackendService; 
    var ngJwtAuthService:NgJwtAuth.NgJwtAuthService; 

    beforeEach(()=>{ 

     module('ngJwtAuth'); 

     inject((_$httpBackend_, _ngJwtAuthService_) => { 
      $httpBackend = _$httpBackend_; 

      if (!ngJwtAuthService){ //dont rebind, so each test gets the singleton 
       ngJwtAuthService = _ngJwtAuthService_; //register injected of service provider 
      } 
     }) 
    }); 

    afterEach(() => { 
     $httpBackend.verifyNoOutstandingExpectation(); 
     $httpBackend.verifyNoOutstandingRequest(); 
    }); 

    it('should be an injectable service',() => { 

     return expect(ngJwtAuthService).to.be.an('object'); 
    }); 

    it('should retrieve a json web token',() => { 

     var user = { 
      email: '[email protected]', 
      password: 'password' 
     }; 

     $httpBackend.expectGET('/api/auth/login', (headers) => { 
      return headers['Authorization'] == 'Basic '+btoa(user.email+':'+user.password); 
     }); 

     var promisedToken = ngJwtAuthService.authenticate(user.email, user.password); 

     promisedToken.then((res) => { 

      console.log('res', res); 
     }); 

     $httpBackend.flush(); 

    }); 


}); 

我得到的結果是

Start: 
    Service tests 
    ✔ should be an injectable service 
    ✖ should retrieve a json web token 
    ✖ "after each" hook 

Finished in 0.055 secs/0.004 secs 

SUMMARY: 
✔ 1 tests completed 
✖ 2 tests failed 

FAILED TESTS: 
    Service tests 
    ✖ should retrieve a json web token 
     Chrome 43.0.2357 (Mac OS X 10.10.3) 
    Error: No pending request to flush ! 
     at Context.<anonymous> (base/test/tmp/test.js?7ade37b85a3e515024b3ce23ba6d9fc4c70ddcc2:104:22) 

    ✖ "after each" hook 
     Chrome 43.0.2357 (Mac OS X 10.10.3) 
    Error: Unsatisfied requests: GET /api/auth/login 
     at Context.<anonymous> (base/test/tmp/test.js?7ade37b85a3e515024b3ce23ba6d9fc4c70ddcc2:86:22) 

,我測試的是

public authenticate(username:string, password:string): ng.IPromise<Object>{ 

    var authHeader = NgJwtAuthService.getAuthHeader(username, password); 

    var requestConfig:ng.IRequestConfig = { 
     method: 'GET', 
     url: this.getLoginEndpoint(), 
     headers: { 
      Authorization : authHeader 
     }, 
     responseType: 'json' 
    }; 

    return this.$http(requestConfig); 
} 

整個項目可以發現問題的方法在https://github.com/spira/angular-jwt-auth

回答

1

好的,我在玩了一段時間之後就解決了它。原來的解決方案是確保$httpbackend沒有爲每個函數重新聲明。這看起來是這樣的:

inject((_$httpBackend_, _ngJwtAuthService_) => { 

    if (!ngJwtAuthService){ //dont rebind, so each test gets the singleton 
     $httpBackend = _$httpBackend_; 
     ngJwtAuthService = _ngJwtAuthService_; //register injected of service provider 
    } 
}) 

,因爲我想每個測試使用相同的服務(該服務是可變的),我必須包括如果塊內的$httpBackend = _$httpBackend_;線。