Unit test your controllers wich repositorys are injected by Autofac

I couldn't find a manual or a blog on the internet about unit-testing and Autofac but it turns out to be quite easy to test your controllers which uses Autofac dependency injection to inject the repository's.

    /// 
    /// Base Test Class wich provides the autofac container
    /// 
    public class BaseTest
    {
        protected IContainer container = null;

        public BaseTest()
        {
            var builder = new ContainerBuilder();
            var business = Assembly.Load("Invoicer.Business");
            builder.RegisterAssemblyTypes(business).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();
            container = builder.Build();
        }
    }

Inherit your Test classes from the BaseTest and do just regular unit-testing

    [TestClass]
    public class AanhefTest:BaseTest
    {
        [TestMethod]
        public void TestAanhefSelectListItems()
        {
            var repository = container.Resolve<Invoicer.Business.Repository.iAanhefRepository>();
            Assert.IsTrue(repository.AanhefSelectListItems(0).Count > 0);
        }
    }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!