I am trying to learn how to use Moq to do unit testing with C#. I setup a simple class, BasicMath that has a function int Multiply(int a, int b)
, the body just returns a * b. I have another class called AdvancedMath which has a function 'int Square(int a)', the body calls Multiply(a,a) and returns the result.
I am trying to write a simple test for AdvancedMath.Square that will assert that BasicMath.Multiply is called with the correct values. I understand that you cannot do this with ordinary public function so I made the functions virtual, but I am still having trouble. Can someone explain to me how this is done? Neither class has any constructor other than the default empty constructor.
Here is my attempt:
class test_Advanced_Functions
{
[Test]
public void test_SquareFunction_Should_return_correct_result()
{
// Arrange
Mock<BasicFunctions> mock = new Mock<BasicFunctions>();
AdvancedFunctions af = new AdvancedFunctions();
// Act
int result = af.Square(5);
//Assert
mock.Verify(d => d.Multiply(5, 5), Times.Exactly(1));
}