Last active
May 25, 2017 21:43
-
-
Save pygeek/7839151523123efa1c5d5f6a9eaef523 to your computer and use it in GitHub Desktop.
Properly testing nested if conditionals.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is an example of how to properly structure your tests when handle nested conditions. | |
# When our test structure represents the code structure, it enables us to better understand the code and more easily identify gaps in our testing coverage. | |
# Take an elseif condition for example | |
if x | |
put x | |
elsif y | |
put y | |
else | |
put z | |
end | |
# The above is shorthand for | |
if x | |
put x | |
else | |
if y | |
put y | |
else | |
put z | |
end | |
end | |
# Test structure for the above two examples with incorrectly nested contexts | |
context when x is true | |
it prints x to the screen | |
context when y is true | |
it prints y to the screen | |
context when y is false | |
it prints z to the screen | |
# This doesn't represent the code structure, this is not only confusing, but it makes it harder to identify missing coverage. | |
# Test structure for the above two examples with correctly nested contexts | |
context when x is true | |
it prints x to the screen | |
context when x is false | |
it does not print x to the screen | |
context when y is true | |
it prints y to the screen | |
context when y is false | |
it prints z to the screen | |
# Looking at this I understand precisely what the logic is. | |
# Example with an additional to level if statement | |
if x | |
put x | |
elsif y | |
put y | |
else | |
put z | |
end | |
if a | |
put a | |
else | |
put b | |
# The above example with correctly nested contexts | |
context when x is true | |
it prints x to the screen | |
context when x is not true | |
it does not print x to the screen | |
context when y is true | |
it prints y to the screen | |
context when y is false | |
it prints z to the screen | |
context when a is true | |
it prints a to the screen | |
context when a is false | |
it prints b to the screen | |
# When contexts are properly nested it's possible to distinguish an nested condition from another top level condition. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment