asp.net core unit testing

Post on 16-Apr-2017

2.169 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

Agenda

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

What’s Next?

Background

Manual Testing

> Nothing new to learn

> Error-prone> Expensive> Slower

Automated Testing

> Less human error> Faster!> Decreasing costs

> Learning curve

Legacy New/Improved

Design Patterns

S.O.L.I.D.

TDD

Refactor

Restructure

• SRP• OCP• LSP• ISP• DIP

Continuous Integration with Agile/Scrum

Getting Started

Unit Testing Frameworks

Types of Automated Tests

Unit Tests

Integration Tests

UI Tests

xUnit.net Test Extensions1. Click Tools Extensions and Updates

2. Search for xunit and install it

Test Project In Solution Explorer:

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

Add New Project

Select Class Libraryunder Visual C#

xUnit.net Config (Test Project)

xUnit.net Config (App Project)

References in Test Project

xUnit.net references

Web App being tested

Writing and Running

Tests

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

[Theory] and [InlineData] for methods with parameters

Test Explorer

Build Solution

Run Tests in Test Explorer

Assert.Equal( ) for Returned Values

Verify actual result against expected result

Note “var” keyword

Assert.Equal( ) for View Names

Verify that expected view equal to returned view

Assert.Throws<Exception>()

Theory/Fact(Skip=“Reason”)]

Temporarily ignore tests with an optional message.

Testing Your Controllers

Correct Views?

Redirect to URL?

Correct Models

?

HTTP Code

?Mock

Depen-dencie

s!

Model Populate

d?

DependencyInjection

& Mocking

Decouple Your Dependencies

Dependency

Injection

Inversion of Control

Set Up Dependency InjectionIn Startup: Add namespace and dependencies

In Application Code:• Constructor Injection• Setter Injection

Moq in Test Project Config

Moq Reference (Test Project)

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

Mocking Example

HINT: Use mock.Object to get mocked object.

Demo

What’s Next?

What About Human Testers?

Smoke Tests

New Features

Edge Cases

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

Improve Your Software ArchitectureViews & ViewModels

Controllers

Service Layer

Repository LayerEntity Framework

(ORM) SQL Server (database)

Functional UI Testing

Code Coverage and Continuous Integration

60 to 80% coverage ok?

Integration Tests

• Databases

• File Systems

• Network Resources

• Web Requests & Responses

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

Agenda

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

What’s Next?

Contact

Email: shchowd@microsoft.com Twitter: @shahedC

top related