I find it hard to write my types for Unity 2 in my unit tests.
Here is a snippet of the tested class:
public class SomeService : ISomeService
{
private int SomeVar { get; set; }
[Dependency]
public ISessionManager SessionManager { get; set; }
public SomeService()
{
SomeVar = SessionManager.Get<int>("SomeVar");
}
}
Here is what I have in my MSTest ClassInitialize method:
private static ISomeService _someService;
private static IUnityContainer _unityContainer;
[ClassInitialize]
public static void MyClassInitialize(TestContext testContext)
{
var mockSessionManager = new Mock<ISessionManager>();
mockSessionManager.Setup(foo => foo.Get<int>(It.IsAny<string>())).Returns(1);
_unityContainer = new UnityContainer()
.RegisterInstance(mockSessionManager.Object)
.RegisterType<ISomeService, SomeService>(
new InjectionProperty("SessionManager"));
_someService = _unityContainer.Resolve<IAdditionalCoveragesService>();
}
For every test method that I have, when I debug and go into the SomeService constructor, it says that SessionManager is null. What am I doing wrong when trying to register my types in Unity?
Note. I use Moq to set up a mock session manager that should be introduced in SomeService.
Thank!!!
source
share