1

I have a simple controller with init function:

 init() {
    this.PositionService.getPagePosition(this.$state.params.name).then(( res ) => {
      this.position = res.data.position;
    })
  }

And my positions service:

  getPagePosition( name ) {
    return this.$http.get(this.appConfig.api.positions + '/' + name);
  }

My test:

describe('position list page', function() {

  var scope,
      $httpBackend,
      controller;

  beforeEach(module('module'));
  beforeEach(inject(function( $controller, $rootScope, _$httpBackend_, _PositionService_ ) {
    scope = $rootScope.$new();
    $httpBackend = _$httpBackend_;
    controller = $controller('PositionsController', {
      $scope          : scope,
      PositionService : _PositionService_,
    });
  }));

  afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  describe('PositionsController', function() {

    it('should get the position', function() {

      $httpBackend.whenGET(/http:\/\/localhost:3000\/positions\/[a-z]+/).respond(200, {
        data: {
          id: 2
        }
      });

      controller.init();
      $httpBackend.flush();
      expect(controller.position.id).to.equal(1);

    });

  });

});

My problem is that i get this error:

 Error: Unexpected request: GET http://localhost:3000/api/positions/undefined
    No more request expected

Why the paramater is undefiend and why i get this error?

1
  • You are no stubbing this.$state.params.name - method getPagePosition() returns undefined Commented Aug 11, 2015 at 9:13

1 Answer 1

1

With Jasmine and ES5 the test will look like:

angular.module('module', [])
  .controller('PositionsController', function(PositionService, $state) {
    this.init = function() {
      PositionService.getPagePosition($state.params.name)
        .then(function(res) {
          this.position = res.data.position;
        }.bind(this))
    }
  }).service('PositionService', function($http, appConfig) {
    this.getPagePosition = function(name) {
      return $http.get(appConfig.api.positions + '/' + name);
    }
  });


describe('Position list page', function() {

  var scope,
    $httpBackend,
    controller;

  beforeEach(module('module'));

  beforeEach(function() {
    angular.module('module').value('appConfig', {
      api: {
        positions: 'http://localhost:3000/positions'
      }
    })
  })

  beforeEach(inject(function($controller, $rootScope, _$httpBackend_, _PositionService_) {
    scope = $rootScope.$new();
    $httpBackend = _$httpBackend_;
    controller = $controller('PositionsController', {
      $scope: scope,
      PositionService: _PositionService_,
      $state: {
        params: {
          name: 'someParamValue'
        }
      }
    });
  }));

  afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  describe('PositionsController:', function() {

    it('Gets the position - init()', function() {
      
      var stubId = 2

      $httpBackend.whenGET(/http:\/\/localhost:3000\/positions\/[a-z]+/).respond(200, {
          position: {
            id: stubId
          }
      });

      controller.init();
      
      expect(controller.position).not.toBeDefined();
      
      $httpBackend.flush();
      
      expect(controller.position).toBeDefined();
      expect(controller.position.id).toEqual(stubId);
    });

  });

});
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-mocks.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.