TDD
Last updated: February 13, 2023
This is in the works! Would you like to help flesh it out? Let's do it!
Test Driven Development (TDD)
…
-
Write a failing test
-
Write the minimum amount of code needed to pass the test
-
Refactor
Why?
https://dtang.dev/2016-01-28-5-ways-unit-testing-can-help-you
Patterns
- Arrange
- Act
- Assert
Test specifications can read like a sentence
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.
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