SlideShare a Scribd company logo
Build Robust Web Apps in the Real World
@shahedC
WakeUpAndCode.com
* aka ASP.NET 5 before RC1
ASP.NET Core Unit Testing
Background
> Getting Started
> Writing and Running Tests
> Dependency Injection & Mocking
What’s Next?
ASP.NET Core Unit Testing
> Nothing
new to learn
> Error-prone
> Expensive
> Slower
> Less human
error
> Faster!
> Decreasing
costs
> Learning curve
Legacy
New/
Improved
• SRP
• OCP
• LSP
• ISP
• DIP
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
Unit Tests
Integration Tests
UI Tests
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
public test class [Fact] for public methods without parameters
[Theory] and [InlineData] for methods with parameters
Build Solution
Run Tests in Test Explorer
Verify actual result against expected result
Note “var” keyword
Verify that expected view equal to returned view
ASP.NET Core Unit Testing
Temporarily ignore tests with an optional message.
Correct
Views?
Redirect
to URL?
Correct
Models?
HTTP
Code?
Mock
Depen-
dencies!
Model
Populated?
ASP.NET Core Unit Testing
Dependency
Injection
Inversion of
Control
In Startup: Add namespace and dependencies
In Application Code:
• Constructor Injection
• Setter Injection
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
Mocks
Pre-programmed with expected results, doesn’t
actually connect to DB, behavior verification.
Fakes
Working examples, but not production-ready
(e..g in-memory database)
Stubs
Provides canned answers
http://martinfowler.com/articles/mocksArentStubs.html
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
ASP.NET Core Unit Testing
Smoke
Tests
New
Features
Edge
Cases
Cost of Adding
Unit Tests
Cost of Not
Having Unit
Tests
Bugs
Features
Expenses
Views & ViewModels
Controllers
Service Layer
Repository Layer
Entity Framework (ORM)
SQL Server (database)
ASP.NET Core Unit Testing
60 to 80% coverage ok?
• Databases
• File Systems
• Network Resources
• Web Requests & Responses
ASP.NET Core Unit Testing
Background
> Getting Started
> Writing and Running Tests
> Dependency Injection & Mocking
What’s Next?
Email: shchowd@microsoft.com  Twitter: @shahedC

More Related Content

ASP.NET Core Unit Testing

Editor's Notes

  1. Title Page: Automated Testing in ASP .NET Core* 1.0 Build Robust Web Apps in the Real World * aka ASP.NET 5 before RC1 By Shahed Chowdhuri Senior Technical Evangelist @ Microsoft Blog: WakeUpAndCode.com Twitter: @shahedC
  2. Agenda
  3. Introduction
  4. Introduction
  5. Introduction
  6. Introduction
  7. Introduction
  8. Agenda
  9. Contact Microsoft email: shchowd@microsoft.com Personal Twitter: @shahedC Dev Blog: WakeUpAndCode.com