visit
Check the happy path to be happy!
TL;DR: Ensure you fail the test when no exception is thrown in invalid conditions.
// Test: firing at an already hit position should not be allowed
const game = new Battleship();
game.fireAt("A3");
// First hit
try {
game.fireAt("A3");
// Firing at the same spot
} catch (e) {
console.assert(e.message === 'Position already hit.',
'The error message should indicate the position is already hit.');
}
// Test: firing at an already hit position should not be allowed
const game = new Battleship();
game.fireAt("A3");
// First hit
try {
game.fireAt("A3");
// Firing at the same spot
// THIS LINE IS IMPORTANT
cnsole.assert(false,
'An exception should have been thrown' .
' for firing at the same position.');
// THIS LINE IS IMPORTANT
} catch (e) {
console.assert(e.message === 'Position already hit.',
'The error message should indicate the position is already hit.');
}
You can detect this smell by looking for try-catch blocks without a failure condition after an action that should throw an exception.
Test cases expecting exceptions should always include assert(false) right after the invalid action.
Remember: AI Assistants make lots of mistakes
Without Proper Instructions | With Specific Instructions |
---|---|
//gzht888.com/how-to-find-the-stinky-parts-of-your-code-part-xv
//gzht888.com/how-to-find-the-stinky-parts-of-your-code-part-xvi
//gzht888.com/how-to-find-the-stinky-parts-of-your-code-part-xxvii
Disclaimer: Code Smells are my opinion.
Time invested in writing tests and refactoring delivers impressive returns in delivery speed, and Continuous Integration is a core part of making that work in a team setting
Martin Fowler
This article is part of the CodeSmell Series on HackerNoon: How to Find the Stinky Parts of your Code