ASP.NET Core Unit Testing

43
By Shahed Chowdhuri Sr. Technical Evangelist Automated Testing in ASP .NET Core* 1.0 Build Robust Web Apps in the Real World @shahedC WakeUpAndCode.com * aka ASP.NET 5 before RC1

Transcript of ASP.NET Core Unit Testing

Page 1: ASP.NET Core Unit Testing

By Shahed ChowdhuriSr. Technical Evangelist

Automated Testing in ASP .NET Core* 1.0Build Robust Web Apps in the Real World

@shahedC

WakeUpAndCode.com

* aka ASP.NET 5 before RC1

Page 2: ASP.NET Core Unit Testing

Session Objectives And TakeawaysSession Objective(s): Explain why you would need automated testsPlan a more robust software architecture Implement automated testing for web apps

Automated tests provide the confidence needed to add new features to software while minimizing bugsNot having any automated tests will actually cost you more in the long run

Page 3: ASP.NET Core Unit Testing

Agenda

Background> Getting Started > Writing and Running Tests> Dependency Injection & Mocking

What’s Next?

Page 4: ASP.NET Core Unit Testing

Background

Page 5: ASP.NET Core Unit Testing

Manual Testing

> Nothing new to learn

> Error-prone> Expensive> Slower

Page 6: ASP.NET Core Unit Testing

Automated Testing

> Less human error> Faster!> Decreasing costs

> Learning curve

Page 7: ASP.NET Core Unit Testing

Legacy New/Improved

Design Patterns

S.O.L.I.D.

TDD

Refactor

Restructure

• SRP• OCP• LSP• ISP• DIP

Page 8: ASP.NET Core Unit Testing

Continuous Integration with Agile/Scrum

Page 9: ASP.NET Core Unit Testing

Getting Started

Page 10: ASP.NET Core Unit Testing

Unit Testing Frameworks

Page 11: ASP.NET Core Unit Testing

Types of Automated Tests

Unit Tests

Integration Tests

UI Tests

Page 12: ASP.NET Core Unit Testing

xUnit.net Test Extensions1. Click Tools Extensions and Updates

2. Search for xunit and install it

Page 13: ASP.NET Core Unit Testing

Test Project In Solution Explorer:

1. Create “test” folder2. Right-click “test”3. Click Add4. Click New Project

Page 14: ASP.NET Core Unit Testing

Add New Project

Select Class Libraryunder Visual C#

Page 15: ASP.NET Core Unit Testing

xUnit.net Config (Test Project)

Page 16: ASP.NET Core Unit Testing

xUnit.net Config (App Project)

Page 17: ASP.NET Core Unit Testing

References in Test Project

xUnit.net references

Web App being tested

Page 18: ASP.NET Core Unit Testing

Writing and Running

Tests

Page 19: ASP.NET Core Unit Testing

Test Class Attributespublic test class [Fact] for public methods without parameters

[Theory] and [InlineData] for methods with parameters

Page 20: ASP.NET Core Unit Testing

Test Explorer

Build Solution

Run Tests in Test Explorer

Page 21: ASP.NET Core Unit Testing

Assert.Equal( ) for Returned Values

Verify actual result against expected result

Note “var” keyword

Page 22: ASP.NET Core Unit Testing

Assert.Equal( ) for View Names

Verify that expected view equal to returned view

Page 23: ASP.NET Core Unit Testing

Assert.Throws<Exception>()

Page 24: ASP.NET Core Unit Testing

Theory/Fact(Skip=“Reason”)]

Temporarily ignore tests with an optional message.

Page 25: ASP.NET Core Unit Testing

Testing Your Controllers

Correct Views?

Redirect to URL?

Correct Models

?

HTTP Code

?Mock

Depen-dencie

s!

Model Populate

d?

Page 26: ASP.NET Core Unit Testing

DependencyInjection

& Mocking

Page 27: ASP.NET Core Unit Testing

Decouple Your Dependencies

Dependency

Injection

Inversion of Control

Page 28: ASP.NET Core Unit Testing

Set Up Dependency InjectionIn Startup: Add namespace and dependencies

In Application Code:• Constructor Injection• Setter Injection

Page 29: ASP.NET Core Unit Testing

Moq in Test Project Config

Page 30: ASP.NET Core Unit Testing

Moq Reference (Test Project)

Page 31: ASP.NET Core Unit Testing

Mocking TermsMocks

Pre-programmed with expected results, doesn’t actually connect to DB, behavior verification.

FakesWorking examples, but not production-ready(e..g in-memory database)

Stubs

Provides canned answers

Reference: http://martinfowler.com/articles/mocksArentStubs.html

Page 32: ASP.NET Core Unit Testing

Mocking Example

HINT: Use mock.Object to get mocked object.

Page 33: ASP.NET Core Unit Testing

Demo

Page 34: ASP.NET Core Unit Testing

What’s Next?

Page 35: ASP.NET Core Unit Testing

What About Human Testers?

Smoke Tests

New Features

Edge Cases

Page 36: ASP.NET Core Unit Testing

Cost of Unit Tests

Cost of Adding Unit

Tests

Cost of Not Having Unit

Tests

Bugs

Features

Expenses

less

no confidence, no time!

reduced piling up

more

more

Page 37: ASP.NET Core Unit Testing

Improve Your Software ArchitectureViews & ViewModels

Controllers

Service Layer

Repository LayerEntity Framework

(ORM) SQL Server (database)

Page 38: ASP.NET Core Unit Testing

Functional UI Testing

Page 39: ASP.NET Core Unit Testing

Code Coverage and Continuous Integration

60 to 80% coverage ok?

Page 40: ASP.NET Core Unit Testing

Integration Tests

• Databases

• File Systems

• Network Resources

• Web Requests & Responses

Page 41: ASP.NET Core Unit Testing

In Review: Session Objectives And TakeawaysSession Objective(s): Explain why you would need automated testsPlan a more robust software architecture Implement automated testing for web apps

Automated tests provide the confidence needed to add new features to software while minimizing bugsNot having any automated tests will actually cost you more in the long run

Page 42: ASP.NET Core Unit Testing

Agenda

Background> Getting Started > Writing and Running Tests> Dependency Injection & Mocking

What’s Next?