TDD

Last updated: February 13, 2023

Introduction

This is in the works! Would you like to help flesh it out? Let's do it!

Test Driven Development (TDD)

  1. Write a failing test

  2. Write the minimum amount of code needed to pass the test

  3. Refactor

Why?

https://dtang.dev/2016-01-28-5-ways-unit-testing-can-help-you

Patterns

  • Arrange
  • Act
  • Assert

https://chuniversiteit.nl/programming/arrange-act-assert

Test specifications can read like a sentence

  1. shoppintcart-test.js ๐Ÿ”’
    describe('The shopping cart', function() {
    	describe('subtotal should', function() {
    		it('be zero if no items are passed in', function() {
    			// Arrange
    			const shoppingCart = new ShoppingCart();
    			// Act
    			const subtotal = shoppingCart.subtotal;
    			// Assert
    			assert.equal(subtotal, 0);
    		});
    	});
    });

    Here’s an example from David Tang’s course on testing with Mocha in Node.js that uses the arrange/act/assert pattern.

    Each testing framework will have different lingo and method names. This example is just an outline. It doesn’t actually import the ShoppingCart. So maybe there need to be a few more parts of this pattern.

  2. shoppintcart-test.js ๐Ÿ”’
    // Arrange everything you'll need below
    import { ShoppingCart } from './services/ShoppingCart.js';
    
    describe('The shopping cart', function() {
    	describe('subtotal should', function() {
    		it('be zero if no items are passed in', function() {
    			// Arrange
    			const shoppingCart = new ShoppingCart();
    			// Act
    			const subtotal = shoppingCart.subtotal;
    			// Assert
    			assert.equal(subtotal, 0);
    		});
    	});
    });

“The shopping cart subtotal should be zero if no items are passed in.”

Stubs, Mocks, Spies, and Dummies

https://blog.bitsrc.io/unit-testing-deep-dive-what-are-stubs-mocks-spies-and-dummies-6f7fde21f710ย