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?
this.$state.params.name- methodgetPagePosition()returns undefined