Problem:
I'm having trouble mocking two properties of my Form using the Moq framework. There are examples of how to do what I'm trying to do out there, but they are all with properties whose type is simple, like string or int.
The Fields property of IMainForm are not being initialized like they do for MainForm, which makes sense to me why (interfaces do not allow this type of initialize to happen). I just don't know how to overcome it.
I have a form defined something like this:
public partial class MainForm : XtraForm, IMainForm
{
...
#region Public Properties
public RichEditDocumentServer DocServer { get; } = new RichEditDocumentServer();
public Dictionary<string, string> Fields { get; } = new Dictionary<string, string>();
#endregion
...
}
In my tests, my fixture's setup is as follows:
...
#region Private Variables
private Mock<IConnector> _arc = new Mock<IConnector>();
private Mock<IMainForm> _arForm = new Mock<IMainForm>();
private MainController _controller;
#endregion
[TestFixtureSetUp]
public void FixtureSetup()
{
_arc.SetupAllProperties();
_arForm.SetupAllProperties();
_controller = new MainController(_arc.Object)
{
View = _arForm.Object
};
}
...
The line that fails in my test (but passes when I run with a UI):
View.Fields.Add(...) // throws NullReferenceException because Fields in null
Intention:
I don't want the public interface to my forms allowing the Fields or DocServer properties to be set to something other than what they are initially set to. But this still allows .Add(...) to be called on the Fields property for instance. I intend to test that adding KeyValuePairs to the Fields is a successful step in making more business-logic-y type stuff to happen.
Question:
What is the Moq way of initializing complex properties with no setter? My tests fail because these properties are null. Thank you.