0
Answer

Unit testing using moq - verify does'n work

Suraj Rai

Suraj Rai

12y
2.5k
1
I have a method that returns void.
say:
 Public void CreateRectangle(double z, double y, IRectangle rectObj)
 {
 Vector3d recPos1 = new Vector3d()
 recPos1.X = 0;
 recPos1.Y = some double vale;
 Vector3d recPos2 = new Vector3d()
 recPos2.X = some double value;
 recPos2.Y = 0;
 rectObj.SetVectors(recPos1, recPos2);
 }
 
now, in my unit test i just want to verify that SetVectors() method has been called on object rectObj.
I have written a moq unit test but it fails. My method looks like this:
 [Test(Description = "")]
  public void TestCreateRectangle()
 {
  double xPos = default(double);
  double yPos = default(double);
  myClass objMyClass = new myClass();
  Mock<IRectangle> iRectangleMock = new Mock<IRectangle>();
  Vector3d recPos1 = new Vector3d();
  recPos1.X = 9;
  recPos1.Y = 0;
  Vector3d recPos2 = new Vector3d();
  recPos2.X = 9;
  recPos2.Y = 0;
  iRectangleMock.Setup(x => x.SetVectors(recPos1, recPos2)).Verifiable();
  objMyClass.CreateRectangle(xPos, yPos, iRectangleMock.Object);
  iRectangleObjectMock.Verify(x => x.SetVectors(recPos1, recPos2));
 }
error: it says - Expected invocation on the mock at least once, but was never performed: x => x.SetVectors(...