@manhnguyenv

Welcome to my blog!

TDD with MVC Applications

July 30, 2018 23:08

TDD with MVC Applications (edit)

TDD With MVC 5 and Entity Framework and Repository pattern

https://code.msdn.microsoft.com/TDD-With-MVC-5-and-Entity-83ade5cf

Developing an ASP.NET MVC 4 Application for TDD using xUnit and Moq

https://github.com/dotnetcurry/tddusingxunit-dncmag-01

ASP.NET MVC 5 application with TDD using Unit testing framework (xUnit.NET) and Moking/Isolation framework (MOQ)

https://github.com/SanjeevForYou/TDDWithxUnitAndMOQ

TDD in ASP.NET MVC Applications with Moq Framework

https://kakimotonline.com/2011/02/13/tdd-in-asp-net-mvc-applications-with-moq-framework/

Unit Testing .NET Application with Moq Framework

https://kakimotonline.com/2011/01/02/unit-testing-net-application-with-moq-framework/

Test Driven Development in ASP.NET MVC 2

https://visualstudiomagazine.com/articles/2015/02/25/code-management-test-driven-dev.aspx

Test Driven Development in ASP.NET MVC 2

https://visualstudiomagazine.com/Articles/2015/05/14/Test-Driven-ASP-NET-MVC-2.aspx

TDD with .NET Core Web API

https://asp.net-hacker.rocks/2017/09/27/testing-aspnetcore.html

https://github.com/JuergenGutsch/blog/blob/master/_source/_posts/2017-09-27-testing-aspnetcore.md

 

 

TDD

July 27, 2018 23:46

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

Categories

Recent posts