Download presentation
Presentation is loading. Please wait.
Published byFelicity Hodge Modified over 9 years ago
1
Unit Testing ASP.Net MVC Craig Berntson Mojo Software Worx
2
Ego stuff Chief Software Gardener Mojo Software Worx Salt Lake City Author, “Continuous Integration in.Net” Columnist, “.Net Curry” eMagazine Conference & event speaker INETA Community Speaker ComponentOne Community Influencer Chief Software Gardener Mojo Software Worx Salt Lake City Author, “Continuous Integration in.Net” Columnist, “.Net Curry” eMagazine Conference & event speaker INETA Community Speaker ComponentOne Community Influencer
3
3
4
Problem with… if switch for foreach while do…while
5
Worse than you think In 1976, G.J. Meyers described a 100-line program that had 10 18 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 In 1976, G.J. Meyers described a 100-line program that had 10 18 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
6
How do you test this? 6
7
Agenda What we won’t talk about TDD Javascript testing Production ready code What we won’t talk about TDD Javascript testing Production ready code What we’ll talk about Setup tests Remove database Keep it simple
8
Demo: Sample Application 8
9
Unit test framework MSTest NUnit xUnit Others MSTest NUnit xUnit Others
10
Unit test runner MSTest NUnit Resharper NCrunch Others MSTest NUnit Resharper NCrunch Others
11
Demo: Our first test 11
12
Default Data access Difficult to test There is no IDbContext DbContext tightly bound to EF (EntityFramework.dll) Difficult to test There is no IDbContext DbContext tightly bound to EF (EntityFramework.dll) ControllerContext Entity Framework
13
Removing database access 1.Repositories 2.Mocks 3.Dependency Injection 1.Repositories 2.Mocks 3.Dependency Injection
14
Repositories Easy to test IRepository Extrapolate away from Context Easy to test IRepository Extrapolate away from Context ControllerRepositoryContext Entity Framework
15
Mocks Allows us to fake the database ControllerRepositoryMock
16
Testing vs. runtime ControllerRepositoryMock Context Entity Framework
17
Class2 How we normally instantiate Class1 new Class2()
18
ClassA Dependency injection Class1 IClass2() ClassA : IClass2
19
Dependency injection Allows for loose coupling Inversion of control (IOC) IOC Container Allows for loose coupling Inversion of control (IOC) IOC Container
20
Ajax & json AJAX calls into the controller JSON returns result AJAX calls into the controller JSON returns result
21
Demo: ajax & json 21
22
Next steps? My blog Art of Unit Testing Udemy.com Katas TDD My blog Art of Unit Testing Udemy.com Katas TDD
23
Review Arrange – Act – Assert In memory Repositories Mocks Dependency Injection Arrange – Act – Assert In memory Repositories Mocks Dependency Injection
24
Questions? craig@mojosoftwareworx.com www.craigberntson.com/blog @craigber craig@mojosoftwareworx.com www.craigberntson.com/blog @craigber 24
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.