Reputation: 101
Is there a way to modify the behavior of a static method to return different parameters?
I am using Gmock for mocking but in this case I am not able to change my code and the methods must stay static
for example
class MyClass
{
public:
static int GetSomething()
{
return -1;
}
};
I need the method to return positive number
Upvotes: 0
Views: 102
Reputation: 36
Disclaimer - I work at Typemock the unit-testing company. You can easily change the behavior of a method with our API, for example:
WHEN_CALLED(MyClass::GetSomething()).Return(15);
In that way, in all of your tests, GetSomehting
will return 15.
It will work on Non Static methods too.
Upvotes: 0
Reputation: 9678
In that case your options are limited, but if it's just for a mock, then just make the method return a static variable instead of a hard coded value.
class MyClass
{
static int somethingValue;
public:
static int GetSomething()
{
return somethingValue;
}
static void SetSomething(int value)
{
somethingValue = value;
}
};
int MyClass::somethingValue = -1;
Upvotes: 2