-
Notifications
You must be signed in to change notification settings - Fork 46
TDD Example
This example goes through the entire process of TDD (Red-Green-Refactor) for writing an Add
macro
-
Stub out the
Add
method with the desired signature (input and output types), but leave the method body blank. (You want to set up the tests before writing any code)Function Add(A As Double, B As Double) As Double End Function
-
Add initial specs for the method
Public Function Specs() As SpecSuite ' Create a new collection of specs Set Specs = New SpecSuite Specs.Description = "Add" ' Report results to Immediate Window ' (ctrl + g or View > Immediate Window) Dim Reporter As New ImmediateReporter Reporter.ListenTo Specs ' Describe the desired behavior With Specs.It("should add 2 numbers") ' Test the desired behavior .Expect(Add(2, 2)).ToEqual 4 .Expect(Add(3, -1)).ToEqual 2 .Expect(Add(-1, -2)).ToEqual -3 End With End Function
-
(Red) Open the immediate window (Ctrl+g or View > Immediate Window) and run the specs (F5) before writing the
Add
method to verify that the tests are currently failing (running properly since no code has been written yet)' === Add === ' X should add 2 numbers ' Expected 0 to equal 4 ' Expected 0 to equal 2 ' Expected 0 to equal -3 ' = FAIL (1 of 1 failed) = ' Failure is a good thing! It means our tests are working since we haven't written Add yet
-
(Green) Write just enough in the
Add
method for the specs to pass and then re-run the specs (F5)Function Add(A As Double, B As Double) As Double Add = A + B End Function ' === Add === ' + should add 2 numbers ' = PASS (1 of 1 passed) = ' Success!
-
(Refactor) Let's say you wanted to expand the Add method to allow for any number of arguments. Start with a refactor of the currently tested behavior.
Function Add(ParamArray Values() As Variant) As Double Add = Values(0) + Values(1) End Function ' === Add === ' + should add 2 numbers ' = PASS (1 of 1 passed) = ' Good!
-
Then add specs for the desired new behavior that will fail initially
Public Function Specs() As SpecSuite Set Specs = New SpecSuite Specs.Description = "Add" Dim Reporter As New ImmediateReporter Reporter.ListenTo Specs With Specs.It("should add 2 numbers") ' Test the desired behavior .Expect(Add(2, 2)).ToEqual 4 .Expect(Add(3, -1)).ToEqual 2 .Expect(Add(-1, -2)).ToEqual -3 End With With Specs.It("should add any number of numbers") .Expect(Add(1, 2, 3)).ToEqual 6 .Expect(Add(1, 2, 3, 4)).ToEqual 10 End With End Function ' === Add === ' + should add 2 numbers ' X should add any number of numbers ' Expected 3 to equal 6 ' Expected 3 to equal 10 ' = FAIL (1 of 2 failed) = ' Failure is ok, we haven't written this functionality yet
-
Then work on
Add
until all of the specs passPublic Function Add(ParamArray Values() As Variant) As Double Dim i As Integer Add = 0 For i = LBound(Values) To UBound(Values) Add = Add + Values(i) Next i End Function ' === Add === ' + should add 2 numbers ' + should add any number of numbers ' = PASS (2 of 2 passed) = ' Woohoo!