ASP.NET 5 & Unit Testing

Post on 28-Jan-2018

4,840 views 1 download

transcript

Build Robust Web Apps in the Real World

@shahedC

WakeUpAndCode.com

Why Unit Testing?

> Getting Started

> Writing and Running Tests

> Mocking

What’s Next?

> Nothing

new to learn

> Error-prone

> Expensive

> Slower

> Less human

error

> Faster!

> Decreasing

costs

> Learning curve

LegacyNew/

Improved

• SRP

• OCP

• LSP

• ISP

• DIP

Source: https://msdn.microsoft.com/en-us/library/jj159342.aspx

Why Unit Testing?

> Getting Started

> Writing and Running Tests

> Mocking

What’s Next?

Unit Tests

Integration Tests

UI Tests

xUnit.net references

Web App being tested

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

Temporarily ignore tests with an optional message.

Correct

Views?

Redirect

to URL?

Correct

Models?

HTTP

Code?

Mock

Depen-

dencies!

Model

Populated?

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

Smoke

Tests

New

Features

Edge

Cases

Cost of Adding

Unit Tests

Cost of Not

Having Unit

Tests

Bugs

Features

Expenses

Dependency

Injection

Inversion of Control

Views & ViewModels

Controllers

Service Layer

Repository Layer

Entity Framework (ORM)

SQL Server (database)

60 to 80% coverage ok?

Why Unit Testing?

> Getting Started

> Writing and Running Tests

> Mocking

What’s Next?

Email: shchowd@microsoft.com Twitter: @shahedC