3

I've two functions funcA() and funcB(), I'm writing unit test cases for $scope.funcA().

Below is the the definition:

function funcB(){
//statements
 console.log("At function B");
};

$scope.funcA() = function(){
funcB(); 
console.log("At function A");
};

Right now while I'm testing my $scope.funcA() is actually calling my funcB(). How to stop this and make a fake call or mock to funcB(); in Jasmine.

Sumit Khanduri
  • 3,619
  • 7
  • 30
  • 40

1 Answers1

0

you can use spyOn() and andCallFake() to achieve it. see one of my earlier answer here . Does Jasmine's spyOn() allow the spied on function to be executed?

hope this helps.

EDIT

for newer versions of jasmine, the snytax would be

spyOn($scope, 'funcB').and.callFake(function() {
      return 'something';
});

for a complete list, see - http://jasmine.github.io/2.0/introduction.html

Community
  • 1
  • 1
Rabi
  • 2,210
  • 17
  • 18
  • Hi could you show me a demo on my given example in my question, I've already tried alot I'll be glad if you can show me this with my example. – Sumit Khanduri Dec 14 '15 at 05:39
  • you should create a plunk/jsfiddle – Rabi Dec 14 '15 at 05:49
  • see funcB is just a basic javascript function i.e., function funcB(){ //statements } _Now what i did is this_ var mockfuncB = jasmine.createSpy("funcB"); spyOn(mockfuncB, 'funcB').and.callFake(function(){ }); what i want to do is i want to make a fake call to funcB() from funcA(). _but it is showing error that funcB() method does not exist_ – Sumit Khanduri Dec 14 '15 at 07:30