Hi,
You are pushing it to the limits here.
Here is how you can do this using a state machine. for example if there are 3 feeds
[TestMethod,VerifyMocks]
public void no_taskbarNotifier_will_be_constructed_if_the_last_instance_of_taskbarnotifer_TaskBarState_not_equal_to_hidden()
{
var mock = MockManager.MockAll<TaskbarNotifier>();
var trackNotifier = mock.TrackInstances();
// make sure that TaskbarState is not hidden when initialized
trackNotifier.InitializeAction = instance => instance.TaskbarState = TaskbarNotifier.TaskbarStates.shown;
var mockApplication = MockManager.Mock<Application>(Constructor.NotMocked);
mockApplication.Do("DoEvents", e =>
{ // 1st loop we have 1 TaskbarNotifier
Assert.AreEqual(1, trackNotifier.Count);
} , e =>
{ // 2nd loop we have 1 TaskbarNotifier - change State
trackNotifier.LastInstance.TaskbarState = TaskbarNotifier.TaskbarStates.hidden;
Assert.AreEqual(1, trackNotifier.Count);
} , e =>
{ // 3rd loop we have 2 TaskbarNotifier
Assert.AreEqual(2, trackNotifier.Count);
}, e =>
{ // 4th loop we have 2 TaskbarNotifier
Assert.AreEqual(2, trackNotifier.Count);
}, e =>
{ // 5th loop we have 2 TaskbarNotifier - change State
trackNotifier.LastInstance.TaskbarState = TaskbarNotifier.TaskbarStates.hidden;
Assert.AreEqual(2, trackNotifier.Count);
}, e =>
{ // 6th loop we have 3 TaskbarNotifier - change State
trackNotifier.LastInstance.TaskbarState = TaskbarNotifier.TaskbarStates.hidden;
Assert.AreEqual(3, trackNotifier.Count);
}
);
MethodUnderTest();
}
Where:
public class TrackInstances<T>
{
public int Count { get; internal set; }
public T LastInstance { get;internal set; }
public Action<T> InitializeAction { get; set; }
}
public static class MockExtend
{
public static TrackInstances<T> TrackInstances<T>(this Mock<T> mock) where T : class
{
var tracker = new TrackInstances<T>() { Count = 0, LastInstance = null };
mock.ExpectConstructor();
// we need to catch the constructor,
// -> as there is a missing API for dynamic return values for constructors
// we will use the MockMethodCalled Event to catch it.
mock.MockMethodCalled += new MockMethodCalledEventHandler((sender, e) =>
{
if (e.CalledMethodName == ".ctor")
{
tracker.LastInstance = (T)sender;
tracker.Count++;
if (tracker.InitializeAction != null)
tracker.InitializeAction(tracker.LastInstance);
}
});
return tracker;
}
public static void Do<T>(this Mock<T> mock, string method, params Action<T>[] actions)
{
foreach (var action in actions)
{
var keepAction = action;
mock.ExpectAndReturn(method, new DynamicReturnValue((p, o) =>
{
keepAction((T)o);
return null;
}));
}
}
}