paint-brush
Code Smell 275 - Missing Test Wrong Path by@mcsee

Code Smell 275 - Missing Test Wrong Path

by Maximiliano ContieriOctober 15th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In testing scenarios where exceptions are expected, it's crucial to include a fail condition to ensure that the test fails if no exception is thrown. Failure to do so can result in silent, faulty tests that pass without indicating underlying issues. The article discusses common problems like missing failure conditions and poor error validation, while providing solutions such as explicit exception validation and using mutation testing. A code example illustrates the difference between proper and improper handling of expected exceptions in tests.
featured image - Code Smell 275 - Missing Test Wrong Path
Maximiliano Contieri HackerNoon profile picture

Check the happy path to be happy!


TL;DR: Ensure you fail the test when no exception is thrown in invalid conditions.

Problems

  • Silent faulty tests
  • Missing failure condition
  • Poor error validation
  • Unclear test outcome
  • Skipped test logic

Solutions

  1. Add failure assertion
  2. Explicit exception validation
  3. Test invalid actions
  4. Catch specific errors
  5. Check all your test paths
  6. Use mutation testing

Context

When writing tests you expect certain cases to throw exceptions. If you forget to add a fail condition when no exception is thrown, your test might pass silently. If breaking some contract doesn't raise an exception, the test will still pass without you noticing the issue. Always include a failure condition to ensure the test fails when the expected exception isn't thrown.

Sample Code

Wrong

// 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.');
}

Right

// 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.');
}

Detection

  • [x]Semi-Automatic

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.

Tags

  • Testing

Level

  • [x]Intermediate

AI Generation

AI generators can create this smell because they often focus on handling exceptions but might miss adding failure conditions when the exception doesn't occur. This leads to the silent passing of faulty tests.

AI Detection

AI can detect this smell if you instruct it to check for missing failure assertions after expected exceptions. It can automatically add the missing condition to ensure tests fail properly when no exception is thrown.


Try Them!

Remember: AI Assistants make lots of mistakes

Without Proper Instructions With Specific Instructions

ChatGPT


Conclusion

You must include a fail condition when testing invalid actions like firing at the same position in Battleship. This ensures the test fails if no exception is thrown, preventing silent errors. Always validate the error message and ensure your tests catch valid and invalid behaviors.

//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.

Credits: Photo by on

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


바카라사이트 바카라사이트 온라인바카라