C# FakeItEasy fake method that have been overrided
-
27-06-2021 - |
سؤال
I have problem with faking my class:
Class A has a method:
protected virtual int method(int argument)
{
implementation
return int;
}
Class B extends class A and overrides the method:
protected override int method(int argument)
{
int result = base.method(argument);
implementation
return result + 1;
}
I need to mock class B and test the method
function. The problem is that I need to avoid calling the base.method(argument)
function. I just need to test my method implementation, and mock that function to return an int.
How can I do it with FakeItEasy? Or other mocking frameworks?
UPDATE
Problem solved by:
making function:
int MethodCaller(int argument)
{
base.method(argument);
}
and executing it in my class B "method" function. Than mocking MethodCaller function
المحلول
You can do it with helper class + one more helper method, but I realy don't like this solution:
public class A
{
protected virtual int method(int argument)
{
return argument;
}
public int result(int argument)
{
return method(argument);
}
}
public class B : A
{
protected override int method(int argument)
{
return baseMetod(0) + 1;
}
protected virtual int baseMetod(int argument)
{
return base.method(argument);
}
}
public class C : B
{
protected override int baseMetod(int argument)
{
return 5;
}
}
so in test you can call new C().Result(10)
لا تنتمي إلى StackOverflow