Reputation: 327
I would like to know is there way i could inject dependencies to the constructor of MS Unit Test class using the Ninject.
Here is the code snippet for repository
public Class StudentRepository : IRepository
{
SchoolContext schoolContext;
public StudentRepository ()
{
schoolContext= new SchoolContext();
}
public IEnumerable<Student> GetAll()
{
return DBContext.Students.ToList();
}
}
here is the code for the IOC configuration
public class IocConfig
{
public static void RegisterIoc()
{
var kernel = new StandardKernel();
kernel.Bind<IRepository>().To<StudentRepository>();
}
}
Here is the code for MS Unit test.
[TestClass]
public Class StudentReposiotryTest
{
public IRepository studentRepository;
[ClassInitialize]
public static void StudentReposiotryInitialize(TestContext context)
{
IocConfig.RegisterIoc();
}
public StudentReposiotryTest(IRepository repository)
{
studentRepository= repository;
}
[TestMethod]
public void GetAllStudentsTest()
{
List<Student> students = studentRepository.GetAll();
Assert.IsTrue(students.Count > 0);
}
}
Upvotes: 1
Views: 768
Reputation: 32725
As far as I know MSTest has no hooks for DI.
But usually you shouldn't need to do so because Unit tests should be kept simple. Just inject Fakes/Mocks into the class under test this can easily be done without IoC container.
Upvotes: 1