VBA-tools / vba-test

Add testing and TDD to VBA on Windows and Mac
MIT License
202 stars 46 forks source link

V2: Evented Approach #14

Closed timhall closed 7 years ago

timhall commented 7 years ago

Evented approach

Events are being added to SpecSuite with the goal of providing more feedback during tests, adding an AfterEach approach for teardown (Fixes #9), and providing a foundation for extensibility (WBProxy and Scenario can be built much more cleanly on top of events).

The following events are fired by SpecSuite:

BeforeEach: Called before each spec in suite (setup) Result(Spec): Called when spec has result AfterEach: Called after each spec in suite (teardown)

Note: BeforeAll and AfterAll type events are best handled in the listener Initialize and Terminate.

Before

Public Function Specs() As SpecSuite
  Set Specs = New SpecSuite
  Specs.BeforeEach "BeforeEach"
  ' no Result or AfterEach

  With Specs.It("...")

  End With

  ' InlineRunner only reports results after suite has run
  ' (also, RunSuite is a misnomer since suite has already run at this point)
  InlineRunner.RunSuite Specs
End Sub

Public Sub BeforeEach()
  ' Run public module sub before each spec
End Sub

After

' ResultFixture.cls
'
Private WithEvents pSpecs as SpecSuite
Public Function ListenTo(Specs As SpecSuite)
  Set pSpecs = Specs
End Function

Private Sub pSpecs_BeforeEach()
  ' Setup fixture
End Sub

Private Sub pSpecs_AfterEach()
  ' Teardown fixture
End Sub

Private Sub pSpecs_Result()
  ' (used by reporters to show results as they happen)
End Sub

' Specs.bas
'
Public Function Specs() As SpecSuite
  Set Specs = New SpecSuite
  Set Reporter = New ImmediateReporter
  Set Fixture = New ResultFixture

  Reporter.ListenTo Specs
  Fixture.ListenTo Specs
  ' -> BeforeEach, Result, and AfterEach are called for each spec

  With Specs.It("...")
    ' -> BeforeEach
    ' .Expect...
  End With
  ' -> Result, AfterEach
End Function

Tasks

Closes #7

robodude666 commented 7 years ago

:+1: Love this update! Much cleaner interaction between the Specs and Reporters.