//Test To Verify Add Never Called on the First Term public void Fibonnaci_GetNthTerm_InputNegative_AssertExceptionThrown() { //Arrange int n = -2; //setup Mock <UnitTests.IMath> mockMath = new Mock <UnitTests.IMath>(); mockMath .Setup(r => r.Add(It.IsAny <int>(), It.IsAny <int>())) .Returns((int x, int y) => x + y); UnitTests.Fibonnaci fibonnaci = new UnitTests.Fibonnaci(mockMath.Object); //Act and Assert Assert.ThrowsException <System.Exception>(() => fibonnaci.GetNthTerm(n)); }
//Test To Verify Add Called Three times on the fifth Term public void Fibonnaci_GetNthTerm_Input4_AssertAddCalledThreeTimes() { //Arrange int n = 4; //setup Mock <UnitTests.IMath> mockMath = new Mock <UnitTests.IMath>(); mockMath .Setup(r => r.Add(It.IsAny <int>(), It.IsAny <int>())) .Returns((int x, int y) => x + y); UnitTests.Fibonnaci fibonnaci = new UnitTests.Fibonnaci(mockMath.Object); //Act int result = fibonnaci.GetNthTerm(n); //Assert mockMath.Verify(r => r.Add(It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(3)); }
//Check the Fifth Term To Make the Test More Robust public void Fibonnaci_GetNthTerm_Input4_AssertResult3() { //Arrange int n = 4; //setup Mock <UnitTests.IMath> mockMath = new Mock <UnitTests.IMath>(); mockMath .Setup(r => r.Add(It.IsAny <int>(), It.IsAny <int>())) .Returns((int x, int y) => x + y); UnitTests.Fibonnaci fibonnaci = new UnitTests.Fibonnaci(mockMath.Object); //Act int result = fibonnaci.GetNthTerm(n); //Assert Assert.AreEqual(result, 3); }