Интеграционные тесты MVC с Unity IoC
Я пытаюсьЕдинство IoC, после использования DI на основе конструктора. Проблема в том, чтобы заставить работать интеграционные тесты.
http://patrick.lioi.net/2013/06/20/streamlined-integration-tests/»
Выполнение ваших интеграционных тестов должно использовать как можно большую часть реальной системы "
Патрик выше описывает настройку IoC внутри тестового проекта MVC .. но яЯ застрял в том, как реализовать
public class HomeController : Controller
{
readonly IWinterDb db;
// Unity knows that if IWinterDb interface is asked for, it will inject in a new WinterDb()
public HomeController(IWinterDb db)
{
this.db = db;
}
public ActionResult Index()
{
var stories = db.Query()
.OrderByDescending(s => s.Rating)
.Include(s => s.StoryType);
return View(stories);
}
Модульные тесты в порядке, переходя в подделку:
[TestMethod]
public void Index_GivenFake_ShouldReturn100Stories()
{
var db = new FakeWinterDb();
db.AddSet(TestData.Stories);
var controller = new HomeController(db);
var result = controller.Index() as ViewResult;
var model = result.Model as IEnumerable;
Assert.AreEqual(100, model.Count());
}
Однако мне нравятся интеграционные тесты, которые тестируют весь стек:
//Integration tests depend on the test data inserted in migrations
[TestClass]
public class HomeControllerTestsIntegration
{
[TestMethod]
public void Index_GivenNothing_ResultShouldNotBeNull()
{
var controller = new HomeController();
var result = controller.Index() as ViewResult;
Assert.IsNotNull(result);
}
проблема: Это не скомпилируется (так как нет конструктора без параметров). И Unity не вызывается для правильной зависимости для HomeController.
Единство подключено:
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
container.RegisterType();
// for authentication
container.RegisterType(new InjectionConstructor());
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
Edit1:
[TestMethod]
public void Index_GivenNothing_ResultShouldNotBeNull()
{
UnityConfig.RegisterComponents();
var controller = UnityConfig.container.Resolve();
var result = controller.Index() as ViewResult;
Assert.IsNotNull(result);
}
Убедиться, что синглтон там.
public static class UnityConfig
{
public static UnityContainer container;
public static void RegisterComponents()
{
container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
//container.RegisterType();
container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface, // Convention of an I in front of interface name
WithName.Default
); // Default not a singleton in Unity
// for authentication
container.RegisterType(new InjectionConstructor());
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
Разоблачение Unity для тестового проекта