TDD with ASP.NET MVC (edit)

  • ASP.NET MVC 5 (Recommended Resources)
  • Entity Framework 6 + Code First
  • AutoMapper
  • Autofac
  • IoC + DI + TDD (Test Driven Development)
  • xUnit and/or NUnit
  • Moq
  • Generic Repository
  • Repository Pattern
  • Unit of Work

Samples

Sample web app for ASP.NET MVC 5, EF 6 Code First, AutoMapper, Autofac and TDD

https://github.com/manhnguyenv/SocialGoal

Full ASP.NET Core 2.0 application with DDD, CQRS and Event Sourcing

https://github.com/manhnguyenv/EquinoxProject

https://github.com/manhnguyenv/MyTecBits-MVC5-Bootstrap3-EF6-DatabaseFirst

https://github.com/manhnguyenv/MyTecBits-MVC5-Bootstrap3

https://github.com/manhnguyenv/MyTecBits-Bootstrap-Empty-MVC

Good Resources

https://github.com/mytecbits/

Questions & Answers

http://mvcaspdotnetinterviewquestions.blogspot.com/2016/02/mvc-aspnet-multiple-choice-interview-questions-and-answers-pdf.html

Others

https://www.c-sharpcorner.com/UploadFile/dacca2/unit-test-using-mock-object-in-dependency-injection/

https://www.codeproject.com/Articles/741207/Repository-with-Unit-of-Work-IoC-and-Unit-Test

MyController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcTddDemo.Models;

namespace MvcTddDemo.Controllers
{
    public class CarDealershipController : Controller
    {
        private ICarDealershipRepository repository;

        public CarDealershipController(
            ICarDealershipRepository repository)
        {
            this.repository = repository;
        }

        public ActionResult List()
        {
            var cars = repository.GetAllCars();
            return View("List", cars);
        }
    }

}

MyControllerTests

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Moq;
using MvcTddDemo.Controllers;
using MvcTddDemo.Models;
using Xunit;
using Xunit.Extensions;
using System.Web.Routing;
using System.Web;
using System.Collections.Specialized;

namespace MvcTddDemo.UnitTests
{
    public class CarDealershipControllerTests
    {
        private Mock<ICarDealershipRepository> repositoryStub;
        private CarDealershipController sut;

        public CarDealershipControllerTests()
        {
            repositoryStub = new Mock<ICarDealershipRepository>();
            sut = new CarDealershipController(repositoryStub.Object);
        }
        [Fact]
        public void List_WhenActionExecute_ReturnModelContainsListOfCars()
        {
            //Arrange
            repositoryStub.Setup(x => x.GetAllCars()).Returns
                (() => new List<Car> { new Car { } });
            //Act
            var result = sut.List() as ViewResult;
            //Assert
            Assert.True(((IEnumerable<Car>)result.Model).Any());
        }
        [Fact]
        public void List_WhenActionExecute_ReturnsViewNameList()
        {
            //Act
            var result = sut.List() as ViewResult;

            //Assert
            Assert.Equal<string>(result.ViewName, "List");
        }
    }
}