angularjs - How to test $http without mock with jasmine and angular-mocks? -
i want make integration test real calls server, so, don't want use $httpbackend module angular-mocks, try this:
beforeeach(inject(function($rootscope,_membersdatasvc_){ service = _membersdatasvc_; })); it('test',function(done){ service.me().then(function(){done();}); });
and service is:
function me() { return $http .get('urlbase/me') .then(mesuccess); function mesuccess(response) { return response.data.members[0]; } }
this never call $http, seems angular-mocks override $http service never made call.
some ideas?
edit 1:
according post: http://base2.io/2013/10/29/conditionally-mock-http-backend/
you can make passthrough $http calls don't want mock, y try this:
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();}) });
but passthrough undefined here.
edit 2:
i read post: http://blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e/, supose stanalone test??, want run karma , jasmine.
this entire 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 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(); }); });
i recomend using ngmidwaytester allows connect real backend, use make integration tests on code level - in between unit , e2e testing:
two types of tests in angularjs (plus 1 more) - full-spectrum testing angularjs , karma
Comments
Post a Comment