我想知道是否可以使用 Ninject 将依赖项注入到 MS 单元测试类的构造函数中。
这是存储库的代码片段
public Class StudentRepository : IRepository
{
SchoolContext schoolContext;
public StudentRepository ()
{
schoolContext= new SchoolContext();
}
public IEnumerable<Student> GetAll()
{
return DBContext.Students.ToList();
}
}
这是 IOC 配置的代码
public class IocConfig
{
public static void RegisterIoc()
{
var kernel = new StandardKernel();
kernel.Bind<IRepository>().To<StudentRepository>();
}
}
这是 MS 单元测试的代码。
[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);
}
}