Giter Club home page Giter Club logo

populatron_javascript's Introduction

Populatron

Objectives

  • Refactor code into testable units
  • Isolate and mock dependencies that use IO
  • Isolate application from entry point
  • Use jest to mock functions
  • Use behavior-based and state-based testing techniques

Rationale

Real-life applications are complex. Naive programmers will create complex monoliths which are hard to test.

Adherents to TDD will create complex applications composed of simple modules.

This exercise gives students an isolated practice problem to hone their skills.

Background

In this repository you will find a csv file containing information on the cities in the world.

You will also find a yarn project with a single entry point and a single integration test.

The test follows the outside in paradigm, and there is currently a passing user-acceptance test.

Now it is time for you to refactor this monolith into smaller classes, writing unit tests for each one as you go.

TODO

  1. Isolate the entry point from the application class. No notion of command line arguments or console output should leak into the application class itself. If this was turned into a web-based app tomorrow, it should be easy.
  2. Isolate file IO from the app. Disk access is brittle (incorrect paths, disk full, access denied). Refactor file IO into its own class so it can be mocked.
  3. Use jest to mock and inject your file IO class.
  4. Isolate your CSV parsing logic from your app by refactoring it into a parser that returns an generic list of objects.

Verification

Below are some questions you can ask yourself to determine if your code is TDD compliant:

  1. Do your classes communicate by passing messages? Or are there deep-seated dependencies leaking between them? A good way to determine this is to look at require statements. Does any file require more than one external dependency? If so, you need to refactor.
  2. Are any of your methods longer than 5-10 lines max? Do they do more than 1 thing? If so, you probably need to break them up into smaller methods.
  3. Are your tests reliant on data that was in the file? If so, you need to alter your code to mock the appropriate classes so that your tests are in charge of the data being processed. Pure unit tests of a CSV reader should involve just a few lines with a few columns.
  4. Do your tests use real models, or did you make deliberately simplified models to test with? Unit tests should use simplified data and models.
  5. If you set a breakpoint in the constructor of all objects, and you run your unit test, does more than one breakpoint get hit? If so, you aren't mocking sufficiently.
  6. Do your methods mutate any variables? Can you refactor them to be immutable?

Remember these questions, because they apply to all code, not just the code you have written for this exercise.

Stretch

  1. Refactor all your tests to use behavior based testing and mock all dependencies.

Final note

The focus of this exercise is on unit tests and mocking because these are often where developers go wrong. However if you only have unit tests, you face other problems:

unit vs integration

Make sure you don't fall into the mockist trap and skip your integration testing.

populatron_javascript's People

Contributors

mattiasl-galvanize avatar pabranch avatar perrybranch-galvanize avatar rachelkoldenhoven avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.