@manhng

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

xUnit Test

July 18, 2018 23:25

xUnit in ASP.NET Core MVC (edit)

  • Moq
  • FluentAssertions -version 4.2.1
  • xUnit -version 2.1.0
  • xUnit.Runner.Dnx -version 2.1.0
  • Microsoft.AspNetCore.TestHost
  • Microsoft.EntityFrameworkCore.InMemory
  • Microsoft.EntityFrameworkCore.Sqlite
  • EfCore.TestSupport

Moq is used for mocking the 3rd party objects or objects beyond the scope of unit test so that unit tests can be written for well effective smaller unit.

  1. Integration testing your asp .net core app with an in memory database
  2. Integration testing your asp .net core app dealing with anti request forgery csrf formdata and cookies 

Unit Testing

https://nance.io/leveling-up-your-dotnet-testing/

https://nance.io/leveling-up-your-dotnet-testing-transactional-integration-testing-in-asp-net-core/

http://www.stefanhendriks.com/2016/04/29/integration-testing-your-dot-net-core-app-with-an-in-memory-database/

https://github.com/aspnet/Docs/tree/master/aspnetcore/mvc/controllers/testing/sample

https://github.com/aspnet/EntityFramework.Docs/tree/master/samples/core/Miscellaneous/Testing

https://docs.microsoft.com/en-us/aspnet/core/test/?view=aspnetcore-2.1

https://stormpath.com/blog/tutorial-entity-framework-core-in-memory-database-asp-net-core

https://gunnarpeipman.com/testing/aspnet-core-ef-inmemory/

https://gunnarpeipman.com/aspnet/dependency-injection-in-asp-net-5/

https://scottsauber.com/2017/10/16/using-asp-net-core-testserver-ef-core-in-memory-db-together/

https://www.thereformedprogrammer.net/using-in-memory-databases-for-unit-testing-ef-core-applications/

https://www.meziantou.net/2017/08/21/testing-an-asp-net-core-application-using-testserver

http://www.dotnetcurry.com/aspnet-core/1420/integration-testing-aspnet-core

Categories

Recent posts