Created
February 7, 2013 20:57
-
-
Save Fodsuk/4734096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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