0

i try to test an angular controller but he returns a promise and i cant really resolve it...what's wrong with my code?

the Angular Controller:

kpi.AboutController = function ($scope, Version) {

    function loadVersion() {
        //$scope.version = Version.getVersion();
        $scope.version = '1.1.0';
    }

    loadVersion();
};

and the jasmine test:

describe('dashboard: AboutController', function() {
beforeEach(module('dashboard'));
var $controller;
beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
}));
it('testing the version number', function() {
    var $scope = {};
    var controller = $controller('AboutController', { $scope: $scope });
    var version = 0;
    console.log($scope.version);
    expect($scope.version).toContain("1.");
});
});

this is working, but when i change the line $scope.version = '1.1.0'; to $scope.version = Version.getVersion(); i receive a promise and i can not rly check it....i tried to resolve it with "then()" function via $scope.version.then(...)but that did not work...what to do?

edit:

the following error occures:

Expected e({ $promise: Object({ $$state: Object({ status: 0 }) }), $resolved: false }) to contain '1.'.
    at Object.<anonymous>

and the Service:

kpi.VersionFactory = function ($resource, appConfig) {
var Version = $resource(thePath, {}, {

    "getVersion": {
        method: "GET",
        url: thePath,
        isArray: false
    }

});

return Version;
};
4
  • can you show the error ? Commented Jul 8, 2015 at 12:49
  • What's the code of the Version being injected in the controller ? This cannot be answered without it. Commented Jul 8, 2015 at 13:10
  • Please show the angular service Version. What you showed is ...Java? Commented Jul 8, 2015 at 13:21
  • i posted the factory Commented Jul 9, 2015 at 10:38

2 Answers 2

1

you need to pass a callback into your test case .

kpi.AboutKPIController = function ($scope, Version) {

    $scope.loadVersion= function () {
      Version.getVersion().then(function(res){
                   $scope.version = res.data;
            })
    }

     $scope.loadVersion();
};
describe('dashboard: AboutKPIController', function() {
  beforeEach(module('dashboard'));
  var $controller;
  beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter                names when matching
    $controller = _$controller_;
 }));
 it('testing the version number', function(done) {
    var $scope = {};
    var controller = $controller('AboutKPIController', { $scope: $scope });
    var version = 0;
    console.log($scope.version);
    $scope.loadVersion().then(function(res)){
             //can test here
             expect($scope.version).toContain("1.");
            done();
      });   
  });

});

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

2 Comments

ty for your answer, but when i do so, i receive TypeError: Version.getVersion(...).then is not a function
which one you need to know? the java service?
1

I expect Version.getVersion(); returns a promise. Your controller implemnentation should look like

function loadVersion() {
    Version.getVersion().then(function(version) {
        $scope.version = version;
    }); 
}

In your test code use $scope.$apply to resolve promises before you perform except.

beforeEach(inject(function(_$controller_, _$rootScope_){

    $controller = _$controller_;
    $rootScope = _$rootScope_;
}));
it('testing the version number', function() {
    var $scope = $rootScope.$new();
    var controller = $controller('AboutKPIController', { $scope: $scope });
    controller.loadVersion();
    $scope.$apply();
    expect($scope.version).toContain("1.");
});

2 Comments

when i do so, i receive TypeError: Version.getVersion(...).then is not a function
@messerbill You didn't show Version.getVersion() . I expected it to return a promise. We can give better answers if you show all the details.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.