380 likes | 724 Views
Unit Testing ASP.Net MVC. Craig Berntson Mojo Software Worx. Thanks to our Sponsors! . To connect to wireless 1. Choose Uguest in the wireless list 2. Open a browser. This will open a Uof U website 3. Choose Login . Ego stuff. Organizer, Utah Code Camp
E N D
Unit Testing ASP.Net MVC Craig BerntsonMojo Software Worx
Thanks to our Sponsors! To connect to wireless 1. Choose Uguest in the wireless list 2. Open a browser. This will open a Uof U website 3. Choose Login
Ego stuff • Organizer, Utah Code Camp • Author, “Continuous Integration in .Net” • Conference & event speaker • INETA Community Speaker • ComponentOne Community Influencer • Columnist, DNC Magazine • 17 time Microsoft MVP • Chief Software GardenerMojo Software WorxSalt Lake City
Problem with… • if • switch • for • foreach • while • do…while
Worse than you think • In 1976, G.J. Meyers described a 100-line program that had 1018 unique paths. • In 1979 he described a much simpler program. It was just a loop and a few IF statements. In most languages, you could write it in 20 lines of code. This program has 100 trillion paths Testing Computer Software, CemKaner
Agenda • What we won’t talk about • TDD • Javascript testing • Production ready code • What we’ll talk about • Setup tests • Remove database • Keep it simple
Unit test framework • MSTest • NUnit • xUnit • Others
Unit test runner • MSTest • NUnit • Resharper • NCrunch • Others
Default Data access • Difficult to test • There is no IDbContext • DbContext tightly bound to EF (EntityFramework.dll)
Removing database access • Repositories • Mocks • Dependency Injection
Repositories • Easy to test • IRepository • Extrapolate away from Context
Mocks • Allows us to fake the database
Class2 • Class1 How we normally instantiate new Class2()
ClassA • Class1 • ClassA : IClass2 Dependency injection IClass2()
Dependency injection • Allows for loose coupling • Inversion of control (IOC) • IOC Container
Selectlist • ViewBag • ViewModel • Automapper
Ajax & json • AJAX calls into the controller • JSON returns result
Next steps? • My blog • Art of Unit Testing • Udemy.com • Katas • TDD
Review • Arrange – Act – Assert • In memory • Repositories • Mocks • Dependency Injection • Automapper
Questions? • craig@craigberntson.com • @craigber • www.craigberntson.com/blog