![Page 1: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/1.jpg)
EMBRACING THE RED BARA TECHNIQUE FOR SAFELY
REFACTORING YOUR TEST CODEM. Scott Ford (@mscottford) | Chief Code Whisperer and CTO | Corgibytes (@corgibytes)
![Page 2: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/2.jpg)
CREDITSLIZ KEOGH ( ) -
MICHAEL FEATHERS ( ) -
@LUNIVORE ORIGINAL ARTICLE
@MFEATHERS ORIGINAL ARTICLE
![Page 3: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/3.jpg)
JARGON ALERTWHAT'S A RED BAR?
![Page 4: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/4.jpg)
GREEN BAR: ALL TESTS PASSING
![Page 5: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/5.jpg)
RED BAR: SOME TESTS FAILING
![Page 6: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/6.jpg)
BEWARE OF OVERCONFIDENCE
A CAUTIONARY TALE
![Page 7: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/7.jpg)
TEST CODE VS PRODUCTION CODE
![Page 8: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/8.jpg)
KEEP YOUR TEST CODE CLEAN
![Page 9: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/9.jpg)
NORMAL TDD CYCLE
![Page 10: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/10.jpg)
ENSURES THAT YOUR TESTS WILL FAIL IF YOUR PRODUCTION CODE BREAKS
ALLOWS YOU TO REFACTOR YOUR PRODUCTION CODE WITH IMPUNITY
![Page 11: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/11.jpg)
RED-BAR CYCLE
![Page 12: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/12.jpg)
RED, REFACTOR, GREENENSURES THAT YOUR TEST CODE CONTINUES TO DETECT THE FAILURE CONDITION WHILE
REFACTORING THE TEST CODE
![Page 13: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/13.jpg)
REDMUTATE THE PRODUCTION CODE
Produces a red barEnsures that your test can fail
![Page 14: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/14.jpg)
REFACTOR
REFACTOR YOUR TESTS
Continue to see the red barEnsures that your tests are still failing
![Page 15: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/15.jpg)
GREENREVERSE THE PRODUCTION CODE MUTATION
Bar should switch to greenYour test should now pass
![Page 16: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/16.jpg)
THINGS THAT CAN GO WRONG
![Page 17: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/17.jpg)
REDI want to refactor a test, but I can't make it fail
![Page 18: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/18.jpg)
REFACTOR
I'm refactoring a test, and it's started passing
![Page 19: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/19.jpg)
GREENI reverted my production change, and my tests are still failing
![Page 20: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/20.jpg)
CALCULATOR EXAMPLE
![Page 21: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/21.jpg)
STARTING POINT
class Calculator def add(left, right) end end
![Page 22: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/22.jpg)
FAILING TEST
describe 'additon' do specify 'adding two numbers that result in 4' do calculator = Calculator.new result = calculator.add(2, 2) expect(result).to eq(4) end end
WATCH IT FAIL
![Page 23: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/23.jpg)
WATCH IT FAIL
![Page 24: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/24.jpg)
MAKING IT PASS
class Calculator def add(left, right) return 4 end end
WATCH IT PASS
![Page 25: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/25.jpg)
WATCH IT PASS
![Page 26: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/26.jpg)
MAKE THE TESTS BETTER
describe 'additon' do specify 'adding two numbers that result in 4' do calculator = Calculator.new result = calculator.add(2, 2) expect(result).to eq(4) end
specify 'adding two numbers that result in 5' do calculator = Calculator.new result = calculator.add(2, 3) expect(result).to eq(5) end end
WATCH THEM FAIL
![Page 27: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/27.jpg)
WATCH THEM FAIL
![Page 28: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/28.jpg)
MAKE THEM PASS
class Calculator def add(left, right) return left + right end end
WATCH THEM PASS
![Page 29: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/29.jpg)
WATCH THEM PASS
![Page 30: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/30.jpg)
LET'S REFACTOR OUR TESTS
![Page 31: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/31.jpg)
BREAK THE PRODUCTION CODEclass Calculator def add(left, right) return 0 # was: left + right end end
WATCH THE TESTS FAIL
![Page 32: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/32.jpg)
WATCH THE TESTS FAIL
![Page 33: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/33.jpg)
APPLY THE REFACTORINGdescribe 'additon' do let(:calculator) { Calculator.new }
specify 'adding two numbers that result in 4' do result = calculator.add(2, 2) expect(result).to eq(4) end
specify 'adding two numbers that result in 5' do result = calculator.add(2, 3) expect(result).to eq(5) end end
VERIFY THAT THE TESTS STILL FAIL
![Page 34: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/34.jpg)
VERIFY THAT THE TESTS STILL FAIL
![Page 35: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/35.jpg)
RESTORE PRODUCTION CODEclass Calculator def add(left, right) return left + right end end
WATCH THE TESTS PASS
![Page 36: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/36.jpg)
WATCH THE TESTS PASS
![Page 37: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/37.jpg)
QUESTIONS?
![Page 38: Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite](https://reader031.vdocuments.us/reader031/viewer/2022030317/58705a7e1a28aba2118b658b/html5/thumbnails/38.jpg)
CONTACT INFO
M. Scott FordTwitter: @mscottfordBlog: http://corgibytes.com/blog