I have a lot of experience in Mocking in another languages like Ruby, but I am new with Moq in C#, I am trying to validate what for me is the most basic mock: to validate that a method is calling the proper method with the proper parameter.
I have this example Subject class
public class Subject
{
int _age;
public Subject(int age)
{
_age = age;
}
public void MainMethod()
{
if(_age <= 13)
{
KidMethod();
}
else
{
AdultMethod();
}
}
public void KidMethod() {}
public void AdultMethod() {}
}
And I am trying to create this Test:
[Test]
public void MainMethod_ShouldCallKidMethod_IfAgeBelow1()
{
var subjectMock = new Mock<Subject>(12);
subjectMock.Verify(subject => subject.KidMethod());
subjectMock.MainMethod();
}
But obviously I am doing something wrong because I get this error:
error CS1061: 'Mock' does not contain a definition for 'MainMethod' and no accessible extension method 'MainMethod' accepting a first argument of type 'Mock' could be found (are you missing a using directive or an assembly reference?)
I think I am missing some basic understanding of Mocking with Moq solving this example will help me to get it.
I have been checking similar Questions in SO but all the ones I've checked were covering more specific cases and I didn't find anyone solving this general simple case.
Update 1
This is compiling:
[Test]
public void MainMethod_ShouldCallKidMethod_IfAgeBelow1()
{
var subjectMock = new Mock<Subject>(12);
subjectMock.Object.MainMethod();
subjectMock.Verify(subject => subject.KidMethod());
}
But now I have the error:
System.NotSupportedException : Unsupported expression: subject => subject.KidMethod() Non-overridable members (here: Subject.KidMethod) may not be used in setup / verification expressions.
Looks like that Moq can not be used with methods that can't be overridden 1
Is there no way to do what I am trying?