Testy integracji MVC z Unity IoC

PróbujęJedność IoC, po użyciu konstruktora DI. Problem polega na próbie uruchomienia testów integracyjnych.

http://patrick.lioi.net/2013/06/20/streamlined-integration-tests/

„Przeprowadzanie testów integracyjnych powinno wykonywać tyle rzeczywistego systemu, na ile jest to możliwe”

Patrick powyżej opisuje konfigurację IoC wewnątrz projektu testowego MVC Unit ... ale utknąłem na drodze do wdrożenia

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<Story>()
                        .OrderByDescending(s => s.Rating)
                        .Include(s => s.StoryType);

        return View(stories);
    }

Testy jednostkowe są w porządku, podając fałszywe:

[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<Story>;

    Assert.AreEqual(100, model.Count());
}

Jednak lubię testy integracyjne, które testują cały stos:

//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);
    }

Problem: To się nie skompiluje (jako żaden konstruktor bez parametrów). I Unity nie jest wywoływane, aby wprowadzić poprawną zależność dla HomeController.

Połącz się z jednością:

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<IWinterDb, WinterDb>();

        // for authentication
        container.RegisterType<AccountController>(new InjectionConstructor());

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}

Edytuj1:

[TestMethod]
public void Index_GivenNothing_ResultShouldNotBeNull()
{
    UnityConfig.RegisterComponents(); 
    var controller = UnityConfig.container.Resolve<HomeController>();

    var result = controller.Index() as ViewResult;

    Assert.IsNotNull(result);
}

Upewniając się, że singleton jest tam.

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<IWinterDb, WinterDb>();

        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<AccountController>(new InjectionConstructor());

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}

Eksponowanie jedności w projekcie testowym

questionAnswers(1)

yourAnswerToTheQuestion