slides - Craig Berntson

Download Report

Transcript slides - Craig Berntson

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
 Author, “Continuous Integration in .Net”
 Conference & event speaker
 INETA Community Speaker
 ComponentOne Community Influencer
 Columnist, DNC Magazine
 17 time Microsoft MVP
 Chief Software Gardener
Mojo Software Worx
Salt 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, Cem Kaner
How do you test this?
6
Agenda
 What we won’t talk about
 What we’ll talk about
 TDD
 Setup tests
 Javascript testing
 Remove database
 Production ready code
 Keep it simple
Demo: Sample
Application
8
Unit test framework
 MSTest
 NUnit
 xUnit
 Others
Unit test runner
 MSTest
 NUnit
 Resharper
 NCrunch
 Others
Demo: Our first test
11
Default Data access
Controller
Context
 Difficult to test
 There is no IDbContext
 DbContext tightly bound to EF (EntityFramework.dll)
Entity
Framework
Removing database access
1.
Repositories
2.
Mocks
3.
Dependency Injection
Repositories
Controller
Repository
 Easy to test
 IRepository
 Extrapolate away from Context
Context
Entity
Framework
Mocks
Controller
Repository
 Allows us to fake the database
Mock
Testing vs. runtime
Controller
Repository
Mock
Context
Entity
Framework
How we normally instantiate
Class1
new Class2()
Class2
Dependency injection
Class1
IClass2()
ClassA : IClass2
ClassA
Dependency injection
 Allows for loose coupling
 Inversion of control (IOC)
 IOC Container
Demo: Removing
Database access
20
Selectlist
 ViewBag
 ViewModel
 Automapper
Demo: SELECTLIST
22
Ajax & json
 AJAX calls into the controller
 JSON returns result
Demo: ajax & json
24
Next steps?
 My blog
 Art of Unit Testing
 Udemy.com
 Katas
 TDD
Review
 Arrange – Act – Assert
 In memory
 Repositories
 Mocks
 Dependency Injection
 Automapper
Questions?
 [email protected]
 @craigber
 www.craigberntson.com/blog