Skip to content

Instantly share code, notes, and snippets.

@Fodsuk
Created February 7, 2013 20:57
Show Gist options
  • Save Fodsuk/4734096 to your computer and use it in GitHub Desktop.
Save Fodsuk/4734096 to your computer and use it in GitHub Desktop.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.ServiceLocation;
using Moq;
namespace ServiceLocatorMocking
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
ServiceLocatorConfig.AddItem<int>(4);
Assert.AreEqual(4, ServiceLocator.Current.GetInstance<int>());
}
[TestMethod]
public void TestMethod2()
{
ServiceLocatorConfig.AddItem<double>(3.3);
Assert.AreEqual(3.3, ServiceLocator.Current.GetInstance<double>());
}
}
public class ServiceLocatorConfig
{
private static Mock<IServiceLocator> ServiceLocatorImpl { get; set; }
static ServiceLocatorConfig()
{
ServiceLocatorImpl = new Mock<IServiceLocator>();
ServiceLocator.SetLocatorProvider(() => ServiceLocatorImpl.Object);
}
public static void AddItem<T>(T objectToReturn)
{
ServiceLocatorImpl.Setup(s => s.GetInstance<T>()).Returns(objectToReturn);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment