I have the following implementation,
public interface IMath {
double Add(double a, double b);
double Subtract(double a, double b);
double Divide(double a, double b);
double Multiply(double a, double b);
double Factorial(int a);
}
public class CMath: IMath {
public double Add(double a, double b) {
return a + b;
}
public double Subtract(double a, double b) {
return a - b;
}
public double Multiply(double a, double b) {
return a * b;
}
public double Divide(double a, double b) {
if (b == 0)
throw new DivideByZeroException();
return a / b;
}
public double Factorial(int a) {
double factorial = 1.0;
for (int i = 1; i <= a; i++)
factorial = Multiply(factorial, i);
return factorial;
}
}
How can I test that Multiply()
is called n times when n's Factorial is calculated?
I'm using NUnit 3 and Moq. Following are the tests that I have already written,
[TestFixture]
public class CMathTests {
CMath mathObj;
[SetUp]
public void Setup() {
mathObj = new CMath();
}
[Test]
public void Add_Numbers9and5_Expected14() {
Assert.AreEqual(14, mathObj.Add(9, 5));
}
[Test]
public void Subtract_5From9_Expected4() {
Assert.AreEqual(4, mathObj.Subtract(9, 5));
}
[Test]
public void Multiply_5by9_Expected45() {
Assert.AreEqual(45, mathObj.Multiply(5, 9));
}
[Test]
public void When80isDividedby16_ResultIs5() {
Assert.AreEqual(5, mathObj.Divide(80, 16));
}
[Test]
public void When5isDividedBy0_ExceptionIsThrown() {
Assert.That(() => mathObj.Divide(1, 0),
Throws.Exception.TypeOf<DivideByZeroException>());
}
[Test]
public void Factorial_Of4_ShouldReturn24() {
Assert.That(mathObj.Factorial(4), Is.EqualTo(24));
}
[Test]
public void Factorial_Of4_CallsMultiply4Times() {
}
}
I'm fairly new to using Moq so I'm not quite getting it at the moment.