1)
Somewhere in TypeMock documentation, I thought I had read it was possible to mock fields. However, I was having trouble mocking "_IsInitialized". For this reason, I added property "IsInitialized", only so I could mock it. Then I neglected to change my unit test. Doron pointed out this problem. Thanks.
Should I be able to mock private data fields?
In the end, decided that mocking this one line wasn't strictly necessary. Instead, I force the value to true and the remainder of the test will reveal if it enters the conditional statement. (see updated test code below)
2)
A colleague suggested that I use mockMonitor.Object, instead of new ExMonitor. This seems to be working, but I don't really understand the difference. Can you enlighten me? Compare the original to the modified version below.
3)
The same colleage also suggested that I can mock FileSystemWatcher without need for a wrapper. To my surprise, this seems successful (see code below). But checking my TypeMock version on this particular computer, I see it is 5.4 (not standard for our organization). Is it correct that this will fail using 4.1 ?
[TestMethod()]
public void StopTest()
{
MockObject<ExMonitor> mockMonitor = MockManager.MockObject(typeof(ExMonitor), Constructor.Mocked) as MockObject<ExMonitor>;
ExMonitor ExMonObj = mockMonitor.Object;
Util_ExMonitorAccessor privateMonitor = new Util_ExMonitorAccessor(ExMonObj);
privateMonitor._isInitialized = true;
MockObject<FileSystemWatcher> mockWatcher = MockManager.MockObject(typeof(FileSystemWatcher)) as MockObject<FileSystemWatcher>;
privateMonitor._watcher = mockWatcher.Object;
mockWatcher.ExpectSet("EnableRaisingEvents").Args(false);
// call the method
ExMonObj.Stop();
}