Exitos
Exitos

Reputation: 29720

How to access 'config' object of interceptor in AngularJS

I am testing an angular interceptor. I want to check something on the config from the context of a jasmine unit test. Here is the code for the test....

it('should set something on the config', function(){

  $http.get('/myEndpoint');
  $httpBackend.flush();

  expect('????? -- I want to check the config.property here');

});

Here is the production code...

  angular.module('app.core').factory('MyInterceptor', function MyInterceptor($injector) {

    return {
      request: function(config) {

        config.property = 'check me in a test';
        return config;
      },

);

My question is how do I check config.property from a test?

Upvotes: 1

Views: 139

Answers (1)

JB Nizet
JB Nizet

Reputation: 691943

The following should work:

var config;
$httpBackend.expectGET('/myEndpoint').respond(200);
$http.get('/myEndpoint').then(function(response) {
    config = response.config;
});
$httpBackend.flush();
expect(config.property).toBe('check me in a test');

But this is almost an integration test. Why not instead create a real unit test:

it('should set something on the config', function() {
    var input = {};
    var config = MyInterceptor.request(input);
    expect(config.property).toBe('check me in a test');
    expect(config).toBe(input);
});

Upvotes: 1

Related Questions