Using Moq To Test An Abstract Class
My answer for the similar question :
As a workaround you can use not the method itself but create virtual wrapper method instead
public abstract class TestAb
{
protected virtual void PrintReal(){
Console.WriteLine("method has been called");
}
public void Print()
{
PrintReal();
}
}
And then override it in the test class:
abstract class TestAbDelegate: TestAb{
public abstract override bool PrintReal();
}
Test:
[Test]
void Test()
{
var mock = new Mock<TestAbDelegate>();
mock.CallBase = true;
mock.Setup(db => db.PrintReal());
var ta = mock.Object;
ta.Print();
mock.Verify(m => m.PrintReal());
}
If you want to mock methods on an abstract class like this, then you need to make it either virtual, or abstract.
The message is because your Test()
method is not public
. Test methods need to be public
. Even after making the test method public
it will fail as you can only verify abstract
/virtual
methods. So in your case you will have to make the method virtual
since you have implementation.