0

I have to write a test-case using karma to the following code.

$scope.getUserDetails = function (param) {
    Api.getData(param).then(function (result) {
        Api.getVal(result).then(function (data) {
            var display = Api.userDetails(result.id, result.name);
            $scope.username = display.name;     
        });
    });
};

But i am facing the problem because of test case fails due to missing parameter from call-back function. I tried many methods. But test-cases failed due to result.id is undefined. Following is just a scaffold of my test-case. 'apiCall' are defined in beforeEach.

it('should test the getData()', function () {
    var user = 'John';
    scope.getUserDetails(123);
    deferred.resolve(user);
    spyOn(apiCall, 'getData').and.returnValue(deferred.promise);
});
Kris
  • 219
  • 1
  • 5
  • 12
  • where are you resolving the promise? Probably you have to mock this variable. – Avraam Mavridis Mar 05 '15 at 15:16
  • i guess you `user` should look more like: `user: {id: '123', name: 'John'}` your `user` from test is the `response` in code – maurycy Mar 05 '15 at 15:19
  • @maurycy : Actually i defined `user` like that. But problem is call back values not getting in inner function. The test-case for the inner function is not written in above test-case. Thanks... – Kris Mar 06 '15 at 09:26

0 Answers0