Caliburn Micro has made a name for itself as one of the easiest to use MVVM Frameworks around. Caliburn Micro comes in with its own built-in service locator, under the IoC class. This class primarly consists of two static methods, IoC.Get<T> and IoC.GetInstance, which allows you to access IoC container from anywhere in code as follows.
- var instance = IoC.Get<LogService>();
- var instance = IoC.GetInstance(typeof(LogService),null);
As one can observe from the code above, these two methods come across as extremely handy for the developer.
Having said so, one of the questions that frequently arises is how do you test a method which consists of either of the above two method calls? To explain the scenario better, let us write a sample method, for which we would write our unit tests.
- public class SampleClass
- {
- public double GetResult(IEnumerable<double> data)
- {
- var sampleService = IoC.Get<SampleService>();
- var result = sampleService.ProcessResult(data);
- // Do further processing
- return finalResult;
- }
- }
The best way to proceed with writing Unit Test would be to let the IoC create an instance of SampleService via Dependency Injection. However, for the sake of example, let us assume we do not have the luxury of making changes to GetResult code. Our sample Unit Test would look as follows:

Join the conversation! Your thoughts help the community grow.