I am asked to upgrade a very old codebase to .net 6 and after upgrading the unit tests no longer pass because the mock library NMock2 doesn't support .net 6. I try to replace the old mock library with Moq but encountered new problems. Since I am no expert in either of the libraries, can someone please help me with the upgrading?
tl;dr
What are the equivalents to
Expect.Exactly(3).On(aHandlerMock).Method("APublicMethod").WithAnyArguments()
and
Expect.Exactly(2).On(aHandlerMock).GetProperty("AOnlyGetterAvailableProperty").Will(Return.Value(false))
Details of the problem:
Test class with NMock2 involved looks like this:
[SetUp]
public void SetUp()
{
this.mockery = new Mockery();
this.messageDelegatorMock = this.mockery.NewMock<IDispatcher>();
this.xHandlerFactoryMock = this.mockery.NewMock<IXHandlerFactory>();
this.xHandlerMock = this.mockery.NewMock<IXHandler>();
this.incomingXDelegate = new IncomingXDelegate(this.messageDelegatorMock)
{
XHandlerFactory = this.xHandlerFactoryMock
};
}
[Test]
public void TestHandleXSimple()
{
TestX testX = new TestX(new Collection<string>());
CY y1 = new CY("1", 0, false);
CY y2 = new CY("1", 1, false);
CY y3 = new CY("1", 2, true);
Expect.Once.On(this.xHandlerFactoryMock)
.Method("createXHandler")
.With(typeof(TestX), "1")
.Will(Return.Value(this.xHandlerMock));
Expect.Exactly(3).On(this.xHandlerMock)
.Method("addMessage")
.WithAnyArguments();
Expect.Exactly(2).On(this.xHandlerMock)
.GetProperty("IsComplete")
.Will(Return.Value(false));
Expect.Once.On(this.xHandlerMock)
.GetProperty("IsComplete")
.Will(Return.Value(true));
Expect.Once.On(this.XHandlerMock)
.GetProperty("Message")
.Will(Return.Value(testX));
Expect.Once.On(this.messageDelegatorMock).Method("Dispatch");
incomingXDelegate.OnX(testX, y1);
incomingXDelegate.OnX(testX, y2);
incomingXDelegate.OnX(testX, y3);
mockery.VerifyAllExpectationsHaveBeenMet();
}
addMessage(object message, IMh info)
is declared in IXHandler
and in the test fixture, class CY
implements IMh
.
My question would be: how can I make use of Moq to mimic the behaviors of NMock2 in the example? Thank you very much for your generous help!