1
我想知道是否有方法可以使用Ninject注入依賴關係到MS單元測試類的構造函數。依賴注入到MS單元測試中使用Ninject的構造函數
這裏是倉庫
public Class StudentRepository : IRepository
{
SchoolContext schoolContext;
public StudentRepository()
{
schoolContext= new SchoolContext();
}
public IEnumerable<Student> GetAll()
{
return DBContext.Students.ToList();
}
}
的代碼片段這裏是國際奧委會配置
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);
}
}