Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method and mock with same class

I have class with 2 methods

class A
{
  void Fun()
  {
    if(FunRet()>0){///} else {///}
  } 
  int FunRet()
  { return 4;}
};

I want to test Fun() method depend on what FunRet returns. So i want to mock FunRet. I rather don't want make FunRet as virtual. How I can do that?

like image 870
userbb Avatar asked Oct 11 '22 20:10

userbb


2 Answers

You can inject intra-class dependencies. In this case, make Fun accept a value instead of computing it:

class A
{
  void Fun(int x)
  {
    if(x>0){///} else {///}
  } 
  int FunRet()
  { return 4;}
};

Then your tests can pass arbitrary values into Fun(). If you need to enforce correct use, write a public version to expose in your API and a private version for testing:

class A {
 public:
  void Fun() { return Fun(FunRet()); }
 private:
  void Fun(int x);  // for testing.
};
like image 80
VladLosev Avatar answered Dec 07 '22 23:12

VladLosev


You could extract the Fun method into a calculator class that implements an interface. You should pass an instance of that interface to class A at constructor.

In testing you could have other classes implementing that interface, that return other values.

This method also have the big advantage, that you seperate the concerns of calculating a value and using the calculated value.

class A {
public:
   A (IFunCalc calc) { m_calc = calc; }
   void Fun { if calc.FunRet() > 4 ... }
private:
   IFunCalc m_calc;
}
class FunCalc : IFunCulc {
public:
   int FunRet { return 4; }
}
class FunCalc4Test : IFunCalc {
public:
   int FunRet { return 27; }
}
like image 34
Chris U Avatar answered Dec 08 '22 01:12

Chris U