unit testing asp.net mvc craig berntson mojo software worx

27
Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Upload: loraine-joseph

Post on 17-Dec-2015

227 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Unit Testing ASP.Net MVC

Craig BerntsonMojo Software Worx

Page 2: 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

Page 3: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

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

Page 4: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Problem with…

if

switch

for

foreach

while

do…while

Page 5: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

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

Page 6: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

How do you test this?

6

Page 7: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

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

Page 8: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Demo: Sample Application

8

Page 9: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Unit test framework

MSTest

NUnit

xUnit

Others

Page 10: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Unit test runner

MSTest

NUnit

Resharper

NCrunch

Others

Page 11: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Demo: Our first test

11

Page 12: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Default Data access

Difficult to test There is no IDbContext

DbContext tightly bound to EF (EntityFramework.dll)

Controller

Context

Entity Framework

Page 13: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Removing database access

1. Repositories

2. Mocks

3. Dependency Injection

Page 14: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Repositories

Easy to test IRepository

Extrapolate away from Context

Controller

Repository

Context

Entity Framewor

k

Page 15: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Mocks

Allows us to fake the database

Controller

Repository

Mock

Page 16: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Testing vs. runtime

Controller

Repository

Mock

Context

Entity Framewo

rk

Page 17: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Class2

How we normally instantiate

Class1 new Class2()

Page 18: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

ClassA

Dependency injection

Class1 IClass2()

ClassA : IClass2

Page 19: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Dependency injection

Allows for loose coupling

Inversion of control (IOC)

IOC Container

Page 20: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Demo: Removing Database access

20

Page 21: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Selectlist

ViewBag

ViewModel

Automapper

Page 22: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Demo: SELECTLIST

22

Page 23: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Ajax & json

AJAX calls into the controller

JSON returns result

Page 24: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Demo: ajax & json

24

Page 25: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Next steps?

My blog

Art of Unit Testing

Udemy.com

Katas

TDD

Page 26: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Review

Arrange – Act – Assert

In memory

Repositories

Mocks

Dependency Injection

Automapper

Page 27: Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx

Questions?

[email protected]

@craigber

www.craigberntson.com/blog