Slide 1

Slide 1 text

The perfect pair Dependency Injection & Unit Testing

Slide 2

Slide 2 text

Chris Ayers 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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Constructor Injection • Clear understanding of dependencies

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Dependency Injection - Scopes Transient Scoped Singleton

Slide 12

Slide 12 text

Dependency Injection - Features Configuration Configuration Scopes Scopes Factory Support Factory Support

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Dependency Injection - DEMO

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 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 18

Slide 18 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 19

Slide 19 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 20

Slide 20 text

Unit Testing – Frameworks Nunit Xunit MStest Moq NSubstitute JustMockIt FakeItEasy

Slide 21

Slide 21 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 22

Slide 22 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 23

Slide 23 text

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

Slide 24

Slide 24 text

Unit Testing - DEMO

Slide 25

Slide 25 text

Questions? I might have answers