SlideShare ist ein Scribd-Unternehmen logo
1 von 27
SELA DEVELOPER PRACTICE
December

-19, 2013

Noam Kfir

Testing ASP.NET MVC Web
Apps

© Copyright SELA software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com
Agenda
Motivation
Testing Approaches
Tools
Common Issues
Motivation
Quality is everybody’s responsibility
The Challenge
As projects grow, complexity increases
The QA should be the last line of defense
Implementing and relying on automation

Tests
Help programmers take responsibility
Are an effective communication mechanism
Are essential to TDD workflows
Testing Challenges
Server and client code are often very different
C# vs. JS
Business logic vs. UI interaction
Single controlled vs. multi-platform environments

Different architectures
Classic web vs. SPA vs. hybrid apps
Multiple network/business topologies

Tooling and API limitations
Black box vs. white box testing
Singletons and other ailments
Our Focus
Visual Studio with a little help from ReSharper
ASP.NET MVC
Mainly TDD workflow, but not only
Solving real problems
And a few other tidbits
Visual Studio
Visual Studio encourages unit testing and TDD
Project Templates
Test Explorer
Navigation
Refactorings
Project Templates
Test Explorer
Built-in to Visual Studio since VS 2012
Designed for multiple workflows, including TDD
Run tests after build
Sorting, filtering, grouping

Uses test adapters to support any framework
MSTest + NUnit, Chutzpah, SpecFlow, and more
Test adapters run side-by-side
Can run JS and C# tests at the same time
Not just for unit tests: Web tests, Coded UI tests…
Creating and Running Unit Tests

Demo
ReSharper
Visual Studio’s little helper – fully integrated
Visual Studio has improved, but ReSharper
makes it better!
Provides more tools for effective unit testing
Test Runner
Easier navigation
Enhanced code completion
Many refactorings, and more flexible
ReSharper

Demo
Types of Tests
Unit tests
Test function inputs and outputs
High-resolution white box tests

Specification tests
Verify that the app behaves according to specs
Often support high-level natural language tests

Web tests
Part of MSTest
Runs as an HTTP client, similar to Fiddler
Has a test designer and supports coded tests
Unit Test Frameworks
MSTest (.NET)
Inspired by an early version of NUnit
Easy to migrate to NUnit (eventually)
Integrates well with TFS

NUnit (.NET) via NUnit test adapter
Feature-rich
Provides fluent assertions
More widespread than MSTest

Jasmine (JavaScript) via Chutzpah test adapter
Very easy to learn behavior-driven framework
NUnit

Demo
SpecFlow
Specification test framework
Tests scenarios and use cases

Uses Gherkin
High-level natural language tests
Supports Hebrew!

Programmers translate natural language to
code
Can run on top of various unit test frameworks
SpecFlow

Demo
Web Tests
Marketed as a testing tools for testers
But very effective for programmers as well

Provide a mechanism for server integration
tests
Client-agnostic
Especially useful for testing HTTP-compliant
and RESTful sites
Web Tests

Demo
Common Issues
Isolating Controller dependencies
The Repository pattern
Working with databases
Crossing layer boundaries
Testing routes
Testing views
Isolating Controller Dependencies
? We need to isolate our unit tests (with
Moq, TypeMock, etc.), but controllers often
depend on singletons or other global
services
Invert your dependencies
Custom ControllerFactory or IoC container
Wrap singletons

Make the test smaller
Not every Controller action has dependencies

Rely on conventions
Use models and model binding and provide your
own
The Repository Pattern
? The Repository pattern is an effective
pattern for managing entities and
databases, but repositories are difficult to
test and mock
Repositories and controllers belong to different
application layers
Test them separately
Controllers should rely on mockable interfaces

Mock intensively
Beyond that, boils down to the database
question
Databases
? Using a real database for unit tests violates
the isolation requirement, but sometimes
you really need to test with real (or fake)
data
Actually, you usually don’t
If you do, consider integration or spec tests
Options:
Mock repository or database replies
Create fake databases, possibly with embedded or
in-memory databases (ORMs make this relatively
easy)
Constrain your tests to the right application layer
Crossing Layer Boundaries
? It’s not always practical to write unit
tests, but integration and spec tests can be
more fragile
Both types of tests are useful and important –
they target different types of issues and have
different goals
Make sure you test a single layer
Regardless of the type of test
Spec and integration tests are also isolated, just at a
different level
Make sure your unit tests focus on a single layer as
well
Testing Routes
? Managing routes, removing conflicts, and
verifying routes find the right controllers is
difficult
Are routes code or configuration?
Consider integration or web tests
Do you test configuration?

If you have custom routes or resolve routes
with custom mechanisms, test (just?) those
mechanisms
Testing Views
? There’s no real way to test views, which is
especially problematic when they have logic
Views are output, rendered, and should not
have to be tested with unit tests
If your views are using ifs or other control flow
code constructs, consider moving the code to
your controller and model
If you really need to test it, use web or UI
tests, but keep in mind that they’ll probably be
fragile
Summary
ASP.NET MVC is designed to be testable
Visual Studio supports almost every workflow
Tooling is vital to successful testing
(ReSharper…)
Many testing problems can be solved by
improving the architecture



Do not rely just on the QA (or your customers)
to test your app
Take responsibility for your code
Questions
Noam Kfir
noamkfir@sela.co.il
@NoamKfir
http://noam.kfir.cc

Weitere ähnliche Inhalte

Andere mochten auch

Laws of test automation framework
Laws of test automation frameworkLaws of test automation framework
Laws of test automation frameworkvodqancr
 
CQRS на практике. В поиске точки масштабирования и новых метафор
CQRS на практике. В поиске точки масштабирования и новых метафорCQRS на практике. В поиске точки масштабирования и новых метафор
CQRS на практике. В поиске точки масштабирования и новых метафорAlexander Byndyu
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with CucumberBrandon Keepers
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 

Andere mochten auch (8)

Laws of test automation framework
Laws of test automation frameworkLaws of test automation framework
Laws of test automation framework
 
CQRS на практике. В поиске точки масштабирования и новых метафор
CQRS на практике. В поиске точки масштабирования и новых метафорCQRS на практике. В поиске точки масштабирования и новых метафор
CQRS на практике. В поиске точки масштабирования и новых метафор
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 

Mehr von Noam Kfir

Agile Mind Games and the Art of Self-Delusion
Agile Mind Games and the Art of Self-DelusionAgile Mind Games and the Art of Self-Delusion
Agile Mind Games and the Art of Self-DelusionNoam Kfir
 
Testers and Coders - Blurring the Lines
Testers and Coders - Blurring the LinesTesters and Coders - Blurring the Lines
Testers and Coders - Blurring the LinesNoam Kfir
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleNoam Kfir
 
TypeScript Modules
TypeScript ModulesTypeScript Modules
TypeScript ModulesNoam Kfir
 
There Is No JavaScript
There Is No JavaScriptThere Is No JavaScript
There Is No JavaScriptNoam Kfir
 
Angular on ASP.NET MVC 6
Angular on ASP.NET MVC 6Angular on ASP.NET MVC 6
Angular on ASP.NET MVC 6Noam Kfir
 
Maximizing UI Automation – A Case Study
Maximizing UI Automation – A Case StudyMaximizing UI Automation – A Case Study
Maximizing UI Automation – A Case StudyNoam Kfir
 
Web components
Web componentsWeb components
Web componentsNoam Kfir
 
HTML5 and the Evolution of the Web
HTML5 and the Evolution of the WebHTML5 and the Evolution of the Web
HTML5 and the Evolution of the WebNoam Kfir
 
Git Workflows
Git WorkflowsGit Workflows
Git WorkflowsNoam Kfir
 
Getting Started with Git: A Primer for SVN and TFS Users
Getting Started with Git: A Primer for SVN and TFS UsersGetting Started with Git: A Primer for SVN and TFS Users
Getting Started with Git: A Primer for SVN and TFS UsersNoam Kfir
 
Building Cross-Platform JavaScript Apps using Cordova
Building Cross-Platform JavaScript Apps using CordovaBuilding Cross-Platform JavaScript Apps using Cordova
Building Cross-Platform JavaScript Apps using CordovaNoam Kfir
 
Telerik Platform
Telerik PlatformTelerik Platform
Telerik PlatformNoam Kfir
 
Profiling JavaScript Performance
Profiling JavaScript PerformanceProfiling JavaScript Performance
Profiling JavaScript PerformanceNoam Kfir
 
Drawing in HTML5 Open House
Drawing in HTML5 Open HouseDrawing in HTML5 Open House
Drawing in HTML5 Open HouseNoam Kfir
 

Mehr von Noam Kfir (17)

Agile Mind Games and the Art of Self-Delusion
Agile Mind Games and the Art of Self-DelusionAgile Mind Games and the Art of Self-Delusion
Agile Mind Games and the Art of Self-Delusion
 
Testers and Coders - Blurring the Lines
Testers and Coders - Blurring the LinesTesters and Coders - Blurring the Lines
Testers and Coders - Blurring the Lines
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
TypeScript Modules
TypeScript ModulesTypeScript Modules
TypeScript Modules
 
There Is No JavaScript
There Is No JavaScriptThere Is No JavaScript
There Is No JavaScript
 
Angular on ASP.NET MVC 6
Angular on ASP.NET MVC 6Angular on ASP.NET MVC 6
Angular on ASP.NET MVC 6
 
Meteor
MeteorMeteor
Meteor
 
Clean code
Clean codeClean code
Clean code
 
Maximizing UI Automation – A Case Study
Maximizing UI Automation – A Case StudyMaximizing UI Automation – A Case Study
Maximizing UI Automation – A Case Study
 
Web components
Web componentsWeb components
Web components
 
HTML5 and the Evolution of the Web
HTML5 and the Evolution of the WebHTML5 and the Evolution of the Web
HTML5 and the Evolution of the Web
 
Git Workflows
Git WorkflowsGit Workflows
Git Workflows
 
Getting Started with Git: A Primer for SVN and TFS Users
Getting Started with Git: A Primer for SVN and TFS UsersGetting Started with Git: A Primer for SVN and TFS Users
Getting Started with Git: A Primer for SVN and TFS Users
 
Building Cross-Platform JavaScript Apps using Cordova
Building Cross-Platform JavaScript Apps using CordovaBuilding Cross-Platform JavaScript Apps using Cordova
Building Cross-Platform JavaScript Apps using Cordova
 
Telerik Platform
Telerik PlatformTelerik Platform
Telerik Platform
 
Profiling JavaScript Performance
Profiling JavaScript PerformanceProfiling JavaScript Performance
Profiling JavaScript Performance
 
Drawing in HTML5 Open House
Drawing in HTML5 Open HouseDrawing in HTML5 Open House
Drawing in HTML5 Open House
 

Kürzlich hochgeladen

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Testing ASP.NET MVC Web Apps

  • 1. SELA DEVELOPER PRACTICE December -19, 2013 Noam Kfir Testing ASP.NET MVC Web Apps © Copyright SELA software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com
  • 3. Motivation Quality is everybody’s responsibility The Challenge As projects grow, complexity increases The QA should be the last line of defense Implementing and relying on automation Tests Help programmers take responsibility Are an effective communication mechanism Are essential to TDD workflows
  • 4. Testing Challenges Server and client code are often very different C# vs. JS Business logic vs. UI interaction Single controlled vs. multi-platform environments Different architectures Classic web vs. SPA vs. hybrid apps Multiple network/business topologies Tooling and API limitations Black box vs. white box testing Singletons and other ailments
  • 5. Our Focus Visual Studio with a little help from ReSharper ASP.NET MVC Mainly TDD workflow, but not only Solving real problems And a few other tidbits
  • 6. Visual Studio Visual Studio encourages unit testing and TDD Project Templates Test Explorer Navigation Refactorings
  • 8. Test Explorer Built-in to Visual Studio since VS 2012 Designed for multiple workflows, including TDD Run tests after build Sorting, filtering, grouping Uses test adapters to support any framework MSTest + NUnit, Chutzpah, SpecFlow, and more Test adapters run side-by-side Can run JS and C# tests at the same time Not just for unit tests: Web tests, Coded UI tests…
  • 9. Creating and Running Unit Tests Demo
  • 10. ReSharper Visual Studio’s little helper – fully integrated Visual Studio has improved, but ReSharper makes it better! Provides more tools for effective unit testing Test Runner Easier navigation Enhanced code completion Many refactorings, and more flexible
  • 12. Types of Tests Unit tests Test function inputs and outputs High-resolution white box tests Specification tests Verify that the app behaves according to specs Often support high-level natural language tests Web tests Part of MSTest Runs as an HTTP client, similar to Fiddler Has a test designer and supports coded tests
  • 13. Unit Test Frameworks MSTest (.NET) Inspired by an early version of NUnit Easy to migrate to NUnit (eventually) Integrates well with TFS NUnit (.NET) via NUnit test adapter Feature-rich Provides fluent assertions More widespread than MSTest Jasmine (JavaScript) via Chutzpah test adapter Very easy to learn behavior-driven framework
  • 15. SpecFlow Specification test framework Tests scenarios and use cases Uses Gherkin High-level natural language tests Supports Hebrew! Programmers translate natural language to code Can run on top of various unit test frameworks
  • 17. Web Tests Marketed as a testing tools for testers But very effective for programmers as well Provide a mechanism for server integration tests Client-agnostic Especially useful for testing HTTP-compliant and RESTful sites
  • 19. Common Issues Isolating Controller dependencies The Repository pattern Working with databases Crossing layer boundaries Testing routes Testing views
  • 20. Isolating Controller Dependencies ? We need to isolate our unit tests (with Moq, TypeMock, etc.), but controllers often depend on singletons or other global services Invert your dependencies Custom ControllerFactory or IoC container Wrap singletons Make the test smaller Not every Controller action has dependencies Rely on conventions Use models and model binding and provide your own
  • 21. The Repository Pattern ? The Repository pattern is an effective pattern for managing entities and databases, but repositories are difficult to test and mock Repositories and controllers belong to different application layers Test them separately Controllers should rely on mockable interfaces Mock intensively Beyond that, boils down to the database question
  • 22. Databases ? Using a real database for unit tests violates the isolation requirement, but sometimes you really need to test with real (or fake) data Actually, you usually don’t If you do, consider integration or spec tests Options: Mock repository or database replies Create fake databases, possibly with embedded or in-memory databases (ORMs make this relatively easy) Constrain your tests to the right application layer
  • 23. Crossing Layer Boundaries ? It’s not always practical to write unit tests, but integration and spec tests can be more fragile Both types of tests are useful and important – they target different types of issues and have different goals Make sure you test a single layer Regardless of the type of test Spec and integration tests are also isolated, just at a different level Make sure your unit tests focus on a single layer as well
  • 24. Testing Routes ? Managing routes, removing conflicts, and verifying routes find the right controllers is difficult Are routes code or configuration? Consider integration or web tests Do you test configuration? If you have custom routes or resolve routes with custom mechanisms, test (just?) those mechanisms
  • 25. Testing Views ? There’s no real way to test views, which is especially problematic when they have logic Views are output, rendered, and should not have to be tested with unit tests If your views are using ifs or other control flow code constructs, consider moving the code to your controller and model If you really need to test it, use web or UI tests, but keep in mind that they’ll probably be fragile
  • 26. Summary ASP.NET MVC is designed to be testable Visual Studio supports almost every workflow Tooling is vital to successful testing (ReSharper…) Many testing problems can be solved by improving the architecture   Do not rely just on the QA (or your customers) to test your app Take responsibility for your code