I'm trying to build a helper class to help with mocking prisms EventAggregator, mostly because I don't want to have to copy / paste the same large mock setup in multiple places.
What I have at the moment is as follows. (apologies, had to hand copy the code over so there may be a couple mistakes)
public Mock<IEventAggregator> CreateEventAggregator()
{
return new Mock<IEventAggregator>();
}
public Mock<TEventType> CreateEvent<TEventType, T>(Mock<IEventAggregator> eventAggregator)
where TEventType : PubSubEvent<T>, nuew()
{
var mockEvent = new Mock<TEventType>();
evengAggregator.Setup(e => e.GetEvent<TEventType>()).Returns(mockEvent.Object);
return mockEvent;
}
public void SetupEventCallback<T, TEventType>(Mock<TEventType> mockEvent)
where TEventType : PubSubEvent<string>
{
Action<T> callback = null;
mockEvent.Setup( p =>
p.Subscribe( It.IsAny<Action<T>>(),
It.IsAny<ThreadOption>(),
It.IsAny<bool>(),
It.IsAny<Predicate<T>>()))
.Callback<Action<T>, ThreadOption, bool, Predicate<T>>((e, t, b, a) => callback = e);
return callback;
}
The problem I'm seeing presents in SetupEventCallback.
If I have this exact code in my test method constructor (with string in place of T), when the callback is used, the test passes. However, when I access it through the helper method, callback remains null.
I've tried hardcoding the helper method to use string instead of T but had the same result.
[TestClass]
public TestClassConstructor
{
helper = EventAggregatorTesthelper();
_mockEventAggregator = helper.CreateEventAggregator();
_mockEvent = helper.CreateEvent<MyPubsubEvent>()
//Doesn't Work
callback = helper.CreateEventCallback<string, MyPubsubEvent>(_mockEvent);
//but the following does work.
mockEvent.Setup( p =>
p.Subscribe( It.IsAny<Action<string>>(),
It.IsAny<ThreadOption>(),
It.IsAny<bool>(),
It.IsAny<Predicate<string>>()))
.Callback<Action<string>, ThreadOption, bool, Predicate<string>>((e, t, b, a) => callback = e);
return callback;
}
[TestMethod]
public void MyTestMethod()
{
callback.Invoke("someString");
Assert.AreEqual("someString", SUT.StringProperty);
}
All I can think is that it's some kind of scoping issue, but not sure how to resolve it.