Slide 1

Slide 1 text

The perfect pair Chris Ayers Dependency Injection & Unit Testing

Slide 2

Slide 2 text

Chris Ayers DevOps Lead @ New Signature Linkedin: /in/chris-l-ayers/ Twitter: @Chris_L_Ayers Projects: https://github.com/Codebytes /UnitTestingDemo https://github.com/Codebytes /DependencyInjectionDemo

Slide 3

Slide 3 text

Topics • Dependency Injection • Why? • SOLID • Design Patterns • Types • Features • DEMO • Unit Testing • Terms • Test Layout and Naming • Mocking • Best Practices • DEMO

Slide 4

Slide 4 text

Why? • Clarity • Maintainability • Reuseability • Modularity • Testability • Use Different Implementations

Slide 5

Slide 5 text

SOLID • Single Responsibility Principle • Open/Closed Principle • Liskov Substituion Principle • Interface Segregation Principle • Dependency Inversion Principle

Slide 6

Slide 6 text

Patterns • Façade • Factory • Strategy • Visitor • Command

Slide 7

Slide 7 text

Façade Pattern Client -complexFacade Façade +ComplexProcess Subsystem 1 +ComplexStep1 Subsystem 2 +ComplexStep2 Subsystem 3 +ComplexStep3 Subsystem 4 +ComplexStep4

Slide 8

Slide 8 text

Factory Pattern Client -shapeFactory <> Shape +draw() ShapeFactory +GetShape() : Shape Circle +draw() Square +draw() Triangle +draw() Implements Implements Implements Creates

Slide 9

Slide 9 text

Strategy Pattern <> CookingMethod +Cook() Dinner -cookingMethod: CookingMethod Microwave +Cook() ToasterOven +Cook() Oven +Cook() Implements Implements Implements Uses

Slide 10

Slide 10 text

Types of Dependency Injection

Slide 11

Slide 11 text

No Dependency Injection • Not using interfaces to reduce coupling • Hard to test • No clear understanding of relationships between different classes

Slide 12

Slide 12 text

Property Injection • Initialization not guaranteed • Can be good for bigger/complex objects

Slide 13

Slide 13 text

Method Parameter Injection • When a dependency is used for only method and is an edge case

Slide 14

Slide 14 text

Constructor Injection • Clear understanding of dependencies

Slide 15

Slide 15 text

Path to Loosely Coupled Classes Tightly Coupled Classes Implement IoC using Factory Pattern Implement DIP by creating Abstractions Implement Dependency Injection Use IoC Container Loosely Coupled Classes

Slide 16

Slide 16 text

Dependency Injection - Scopes Transient Scoped Singleton

Slide 17

Slide 17 text

Dependency Injection - Features Composition Root Configuration Scopes Factory Support

Slide 18

Slide 18 text

Dependency Injection - Frameworks .Net Core Unity Castle Windsor Ninject AutoFac SimpleInjector Spring.NET StructureMap

Slide 19

Slide 19 text

Dependency Injection - DEMO

Slide 20

Slide 20 text

Resources DEPENDENCY INJECTION IN ASP.NET CORE - HTTPS://BIT.LY/34UR0IB FRAMEWORK-PROVIDED SERVICES - HTTPS://BIT.LY/355G0YV IDISPOSABLE - HTTPS://BIT.LY/2VSH9SJ

Slide 21

Slide 21 text

Decoupling is essential for unit testing. DI is a great way to achieve decoupling. Unit Testing

Slide 22

Slide 22 text

Unit Testing – Terms (SUT) System Under Test or (CUT) Class Under Test Test Doubles Fake Mock Stub Spy Dummy Objects

Slide 23

Slide 23 text

Unit Testing - Architectures • Easy to Test Code • Inversion of Control (IoC) • Interface Segregation • Functional Programming • Hard to Test code • It is tightly coupled to the concrete implementation. • It violates the Single Responsibility Principle. • It lies about its dependencies. • It is hard to understand and maintain.

Slide 24

Slide 24 text

Unit Testing - Layout Split up tests by assembly/project. • Ex: if you have two libraries: foo.dll, bar.dll • have 2 unit test assemblies: foo.tests.dll, bar.tests.dll Try to have at least 1 test class per class • Ex: if you have two classes in project foo: a.cs, b.cs • have 2 unit test classes in project foo.tests: aTests.cs, bTests.cs • If there are a lot of methods, or tests for some methods, split it up if there are a too many tests in one class. Use folders to mirror layout and namespaces of target project

Slide 25

Slide 25 text

Unit Testing - Naming Name test class for the class under test • Foo.cs -> FooTests.cs [UnitOfWork_StateUnderTest_ExpectedBehavior] Examples • public void GetLines_twoLineString_returnsTwoStrings() • public void GetNonWhitespaceLines_null_Throws() • public void ReplaceMultiple_stringWithHashNonMatchingMappings_String() • public void ReplaceMultiple_stringMatchingMappings_UpdatedString()

Slide 26

Slide 26 text

Unit Testing – Frameworks Nunit Xunit MStest

Slide 27

Slide 27 text

Mocking – Frameworks Moq NSubstitute JustMock FakeItEasy

Slide 28

Slide 28 text

Unit Testing – Gotchas Usually only works for Interfaces or Virtual Methods. Statics can be difficult; refactoring can help or Microsoft Fakes Mocking can make assumptions about boundaries

Slide 29

Slide 29 text

Unit Testing – Best Practices AAA Arrange Act Assert One Assert Per Test Method Avoid Test Interdependence Keep It Short, Sweet, and Visible Recognize Test Setup Pain as a Smell Add Them to the Build

Slide 30

Slide 30 text

Unit Testing – What is a good test? Fast Isolated Repeatable Self- Checking Timely

Slide 31

Slide 31 text

Unit Testing - DEMO

Slide 32

Slide 32 text

Questions? I might have answers