mercredi 18 novembre 2015

Testing services from $resource using factory with Karma

I'm new to karma and I'm trying to run tests in order to check if my factories are set to the proper value returned by the API.

apiServices.js :

'use strict';

angular.module('balrogApp.services', ['balrogApp.config'])
  .factory('Requests', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
    return $resource(balrogConfig.backend + '/requests/:id', {id: '@id'});
  }])
  .factory('Projects', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
    return $resource(balrogConfig.backend + '/projects/:id', {id: '@id'}, {'update': { method:'PUT' }});
  }])
  .factory('Users', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
    return $resource(balrogConfig.backend + '/users/:id', {id: '@id'});
  }]);

config.js :

'use strict';

angular.module('balrogApp.config', [])
  .constant('balrogConfig', {
    'backend': 'http://ift.tt/1H8fCKO'
  });

Now, I've been through a few articles about $resource and karma but I didn't really get how to set up the unit tests for my case.

Here is the skeleton of my test file :

describe("Services test", function () {
  var factory;

  beforeEach(function () {
    module("balrogApp.services");

    inject(function (_factory_) {
      factory = _factory_;
    });
  });

  it("Factory must be defined", function () {
    expect(factory).toBeDefined();
  });
});

I also tried a few things with $httpBackend but couldn't make it neither.

How to make this work and inform me if my services are working. Where should I specify the service I want to test ?

But also, how to also check if the services are returning the expected response from the API ?

Aucun commentaire:

Enregistrer un commentaire