2015-05-11 20 views
6

Voglio fare un test di integrazione con le chiamate reali al mio server, così, io non voglio usare il modulo $ httpBackend da angolari-prende in giro, così cerco questo:

beforeEach(inject(function($rootScope,_MembersDataSvc_){ 
    service = _MembersDataSvc_; 
})); 

it('test',function(done){ 
    service.me().then(function(){done();}); 
}); 

e il servizio è :

function me() { 
     return $http 
     .get('urlBase/me') 
     .then(meSuccess); 

     function meSuccess(response) { 
      return response.data.members[0]; 
     } 
    } 

Questo non chiamare il $ http, sembra che angolari-schernisce sostituiscono il servizio di $ http un mai ha chiamato.

Alcune idee?

EDIT 1:

Secondo questo post: http://base2.io/2013/10/29/conditionally-mock-http-backend/

si può fare un passthrough per questo $ http chiamate che non si vuole prendere in giro, quindi y provare questo:

var service; 
    var scope; 
    var $httpBackend; 

    beforeEach(inject(function($rootScope,_MembersDataSvc_,_$httpBackend_){ 
     service = _MembersDataSvc_; 
     scope = $rootScope.$new(); 
     $httpBackend = _$httpBackend_; 
    })); 

it('test',function(done){ 
     //this.timeout(10000); 
     $httpBackend.whenGET(/views\/\w+.*/).passThrough(); 
     $httpBackend.whenGET(/^\w+.*/).passThrough(); 
     $httpBackend.whenPOST(/^\w+.*/).passThrough(); 
     service.me().then(function(response){console.log(response);done();}); 
     scope.$apply(); 
     //service.getDevices(member).then(function(response){console.log(response);done();}) 
    }); 

Ma il passThrough non è definito qui.

EDIT 2:

ho letto questo post: http://blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e/, ma io supose che è un test stanalone ??, voglio correre con il karma e gelsomino.

Questo è il mio intero test.

describe('integration test', function() { 

    beforeEach(function() { 
     module('MyAngularApp'); 
    }); 

    var service; 
    var scope; 
    var $httpBackend; 

    beforeEach(inject(function($rootScope,_MembersDataSvc_,_$httpBackend_){ 
     service = _MembersDataSvc_; 
     scope = $rootScope.$new(); 
     $httpBackend = _$httpBackend_; 
    })); 

    it('test for test',function(done){ 
     $httpBackend.whenGET(/views\/\w+.*/).passThrough(); 
     $httpBackend.whenGET(/^\w+.*/).passThrough(); 
     $httpBackend.whenPOST(/^\w+.*/).passThrough(); 
     service.me().then(function(response){console.log(response);done();}); 
     scope.$apply(); 
    }); 
}); 
+0

È necessario utilizzare parti del test E2E https://docs.angularjs.org/api/ngMockE2E. Inoltre, guarda la guida e2e https://docs.angularjs.org/guide/e2e-testing – Chandermani

+0

@Chandermani il fatto è che E2E usa i mock per $ http, voglio che il mio servizio faccia la vera chiamata al mio server, non il finto. – cmarrero01

+0

Non è vero, puoi effettuare chiamate reali nello scenario E2E. La derisione deve essere fatta esplicitamente – Chandermani

risposta