how to write a testable code

Post on 17-Feb-2017

116 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

HOW TO WRITE TESTABLE CODES.O.L.I.D.

HOW TO WRITE TESTABLE CODE

What we will discuss today…

• What we won’t talk about• Some terminology• Single Responsibility• Open/Closed• Liskov Substitution Principle• Interface Segregation• Dependency Inversion• Review• Questions

INTRODUCTION

Andrew TrebbleSenior Developer

OPIN Software

• Why to test• How to write tests• What to test• How to integrate tests into your workflow• How to do Object Oriented Programming

WHAT WE WONT TALK ABOUT

S.O.L.I.D

WHAT WE WILL TALK ABOUT TODAY

But before we start…some terminology

• Class• Instance• Inheritance• Base class• Abstract class• Concrete class• Interface

OBJECT ORIENTED PROGRAMMING TERMS

Unit Tests• Tests a single class in complete isolation• All inputs are fake

Integration Tests• Test the components together to make sure they play well with each other• Test the interaction of your code with other parts of the system

Functional Tests• Test to make sure the system actually does what it is supposed to do• Run with a full system behind it

TYPES OF TESTS

What does that mean?• Your objects should do only one thing• It should be the only object in the code base that does that one job

S.O.L.I.D: SINGLE RESPONSIBILITY

Why?• Reduces code complexity• Ensures that bugs are isolated and easier to find and fix• Reduces the number of tests that you need to write to cover the class

S.O.L.I.D: SINGLE RESPONSIBILITY

Open? Drupal’s already open isn’t it?• Not that kind of open• Open to extension• Closed to change

S.O.L.I.D: OPEN / CLOSED

Why go to the trouble• Reduce the number of regressions you experience• Makes the code more mockable• No need to rewrite tests to account for the new functionality

S.O.L.I.D: OPEN / CLOSED

Oh yeah, of course! Goes without saying… What’s the Liskov Substitution Principle?• First proposed by Barbra Liskov of MIT in the late 80’s• States that in a computer program, if S is a subtype of T, then objects of type T may be replaced with

objects of type S without altering any of the desirable properties of that program• Makes code more resilient to changes in it’s dependencies

S.O.L.I.D: LISKOV SUBSTITUTION PRINCIPLE

Makes sense. Can you expand on why I need this, please?• Makes your classes independent of the implementations of their dependencies• Makes mocking possible

S.O.L.I.D: LISKOV SUBSTITUTION PRINCIPLE

I’m getting tired of trying to come up with ways to say “What’s that”?• A class should have as few methods as is feasible to implement its interface• More small interfaces instead of few big interfaces

S.O.L.I.D: INTERFACE SEGREGATION

Once again, Why?• Smaller interfaces are easier to write tests for.• Smaller interfaces have less reason to change

S.O.L.I.D: INTERFACE SEGREGATION

Please tell me more• The complement of the Liskov Substitution Principle• Dependencies should be abstract base classes or interfaces. Never a concrete implementation of one

of those• Forces loose coupling

S.O.L.I.D: DEPENDENCY INVERSION

Go ahead and inform me of the why• Loose coupling helps enforce dependency injection• Makes your classes independent of the exact types of its dependencies• Makes mocking possible through dependency injection

S.O.L.I.D: DEPENDENCY INVERSION

Dependency Injection• Make your classes dependent on abstract base classes or interfaces• Don’t ever instantiate another object inside of your object• Don’t rely on anything from outside your class unless it was passed in• Pass in all dependencies as instantiated objects• Your hooks will basically become just a bunch of setup that then pass to a object to

do the heavy lifting

REVIEW

Mocking• Write mock classes that implement the same interface or extend the same base class.• Mock objects do not do anything. They only pretend to.• Mock objects return the data that will put your class through it’s paces. You may need multiple tests

to ensure all code paths are executed and tested.

REVIEW

• Monthly meetup• First Wednesday of every month• From 6pm to 8pm• New and exciting topics• Expert speakers and presenters from the

community• Meet new people passionate about Drupal• Get support from the local community• FREE pop, beer & pizza

DRUPALYOW

OPIN Software is the company behind The Open Mic Podcast, which is released bi-weekly and discusses anything & everything Drupal!

We will be hosting a live podcast today at 4pm, in the business track room, and welcome everyone to attend and contribute by discussing what you learned today and what you are hoping for from the community in the future.

To listen to The Open Mic Podcast, please visit:

http://theopenmic.libsyn.com/

THE OPEN MIC PODCAST

OPIN Software is offering free Drupal 8 training sessions to the attendees of DrupalCamp Ottawa. Sessions will include:

• Drupal 8 Fundamentals• Drupal 8 Module Development• Drupal 8 Theming

If you are interested, please visit the OPIN booth upstairs.

FREE DRUPAL 8 TRAINING

THANK YOU!

QUESTIONS?

Go forth and Rock!

top related