# Code Smell 104 - Assert True

*Asserting against booleans makes error tracking more difficult.*

> TL;DR: Don't assert true unless you are checking a boolean

# Problems

- Fail Fast Principle

# Solutions

1. Check if the boolean condition can be rewritten better

2. Favor assertEquals

# Context

When asserting to a boolean our test engines cannot help us very much. 

They just tell us something failed.

Error tracking gets more difficult.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/c54f0b1ee42d6a1aff640507e0bdf625)
```php
<?

final class RangeUnitTest extends TestCase {
 
  function testValidOffset() {
    $range = new Range(1, 1);
    $offset = $range->offset();
    $this->assertTrue(10 == $offset);    
    //No functional essential description :(
    //Accidental description provided by tests is very bad
  }  
}

// When failing Unit framework will show us
//
// 1 Test, 1 failed
// Failing asserting true matches expected false :(
// () <-- no business description :(
//
// <Click to see difference> - Two booleans
// (and a diff comparator will show us two booleans)
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/edf0b1c3339451662bb000055ef5d782)
```php
<?

final class RangeUnitTest extends TestCase {
 
  function testValidOffset() {
    $range = new Range(1, 1);
    $offset = $range->offset();
    $this->assertEquals(10, $offset, 'All pages must have 10 as offset');    
    //Expected value should always be first argument
    //We add a functional essential description 
    //to complement accidental description provided by tests
  }  
}

// When failing Unit framework will show us
//
// 1 Test, 1 failed
// Failing asserting 0 matches expected 10
// All pages must have 10 as offset <-- business description
//
// <Click to see difference> 
// (and a diff comparator will help us and it will be a great help
// for complex objects like objects or jsons)
```

# Detection

[X] SemiAutomatic 

Some linters warn us if we are checking against boolean after setting this condition.

We need to change it to a more specific check.

# Tags

- Test Smells

# Conclusion

Try to rewrite your boolean assertions and you will fix the failures much faster.

# Relations

%[https://maximilianocontieri.com/code-smell-101-comparison-against-booleans]

%[https://maximilianocontieri.com/code-smell-07-boolean-variables]

# More Info

- [Fail Fast](https://maximilianocontieri.com/fail-fast)

# Credits

Photo by <a href="https://unsplash.com/@joeldevriend">Joël de Vriend</a> on <a href="https://unsplash.com/s/photos/truth">Unsplash</a>  

* * *

> I've finally learned what 'upward compatible' means. It means we get to keep all our old mistakes.

_Dennie van Tassel_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
