2

I am really struggling with for few hours now and can't just understant what's wrong with here's my code My service:

(function(){
  'use strict';

  angular
    .module('app')
    .factory('register', register);

    register.$inject = ['$http'];

    function register($http){
      var service = {
          post: post
      };

      return service;
      /////////////////////

      function post(data){
        return $http.post('/user/register', data)
            .then(registerSuccess)
            .catch(registerError);

            function registerSuccess(response){
              return response;
            }

            function registerError(error){
              return error;
            }
      }
    }

})();

and the spec file

    describe('Register service', function(){

  beforeEach(module('app'));

  var service;

  beforeEach(inject(function($http, $httpBackend, _register_) {
    http = $http;
    httpBackend = $httpBackend;
    service = _register_;
  }));

    it('check if register service exist', function(){
      expect(service).toBeDefined();
      expect(service.post()).toBeDefined();
    });

    it('rrrr', function(){
      httpBackend.expectPOST('/user/register', {u: 'xyz', password: 'pass' })
          .respond(200, {'status': 'success'});

      service.post({u: 'xyz', password: 'pass' })
          .then(function(data){
              expect(data.status).toBe(200);
          });
      httpBackend.flush();
    });

});

If anyone can help me to understand why i am having this error

Error: Unexpected request: GET src/app/user/user.html

Thank you...

adam
  • 555
  • 1
  • 5
  • 17

2 Answers2

1

The app is fetching the templates for your routes/components/directives using XHR requests. The best way of getting around this is to use $templateCache.

You can use this preprocessor with karma to put the templates into a cache.

See this answer for more info.

Community
  • 1
  • 1
Amygdaloideum
  • 3,683
  • 2
  • 13
  • 16
1

The simplest solution is to use RegExp('.*.html'):

//setup backend so all .html requests get an 200 response
httpBackend
        .whenGET(new RegExp('.*.html'))
        .respond(function(){ return [200, 'XXX', {}] });

//setup spec specific behavior
httpBackend
        .expectPOST('/user/register', {u: 'xyz', password: 'pass' })

Explanation:
This makes sure all .html GET requests are answered with 200 status reponse.
This will also make your 'Unexpected request' error go away.

Hope it helps.

hannes neukermans
  • 12,017
  • 7
  • 37
  • 56