I am using Moq (which I am very new too as well as TDD period). And I wanted to Moq up an instance of my main Winforms form so I could test a few methods on there. Is this possible? Its constructor takes an object of Assembly().
I was trying the following attempts unsuccessful:
var mockMainForm = new Mock<MainForm>();
mockMainForm.Setup(x => x.Assembler).Returns(new Assembly());
return mockMainForm.Object;
But I can’t access any properties or methods on this object once it is returned. Is this possible to do?
But I get errors that ( failed: System.ArgumentException : Expression is not a method invocation: x => x.Assembler
at Moq.ExpressionExtensions.ToMethodCall(LambdaExpression expression))
Mocking using Moq will only mock the interfaces and virtual methods of a class.
Assemblerproperty needs to be defined asvirtual.In any case Mocking a Windows Form is not a good mocking – it has a big bag of WIN32 stuff which make your tests brittle. If you need to unit test and mock a form, create an interface that your form needs to implement and mock that for objects that need to interact with your form.
then mock the interface and not the form: