But first things first – let’s talk about invoking events using Typemock Isolator…
Invoking events using IsolatorLet’s say I have a special kind of calculator that due strange consequences known only to the product manager need to work via events. The calculator shell “listen” for events from an “input provider”.
Presented with the code below – how can you test it?
Invoking event is a simple as long as you can reach the instance that holds the event – which is not the case if you use Isolate.Swap
Invoking events on future instancesWhat happens if we need to invoke event on an instance that is created deep inside the production code? at work we use timers and need to test behavior dependent on timer elapsed event – just like in the next class:
Testing it should be easy – we have Isolate.Swap and we know how to invoke events, so it shouldn’t be hard to come up with the following test:
The solution – reflectionGur Kashi has come up with a simple solution to this issue – why not use reflection to get the field and use it to invoke the event – the cool thing about Isolator is that it can invoke events on “real” objects as well not just fakes.
With the aid of the following utility method – the class has become testable:
That’s it. The only downside of this solution is that it’s string based and the test might break if the field name is changed – so make sure you have a good error message if the fields was not found.