I am currently in a failing test state:
I see that the expected and actual values are not the same type in the test.
This means that I asserted a 1 of type Long would be a 1 of type Integer. I believe that’s the default type of 1. VBA is usually pretty nice in that it converts variables to the needed type as you use them, but RubberDuck is set to strongly check my types meaning they have to be excplicitly the same.
Since my method in FormListener returns a number of type Long, my test needs to compare it to a type of Long. Here is the offending line of code:
Assert.AreEqual FormListenerTest.TimesFieldChanged("TestText"), 1
So if I change it using the CLng() function which converts the passed variable to a Long, the new code looks like this:
Assert.AreEqual FormListenerTest.TimesFieldChanged("TestText"), CLng(1)
And now I expect it to pass. And yay, I’m right!
Ok, I am now at the refactor step. I do see a bunch of repeated code in my module. At each new test I am creating a variable called “FormListenerTest As FormListener” and then I am doing a “Set FormListenerTest = New FormListener”
I think I will move these lines to the TestInitialize module to run them to have a fresh FormListener object for every test. I also destroy the FormListener object at the end of every test so far so I will also refactor that into the TestCleanup module. Note: I am using module interchangeably with the word sub. Also, the comments are kind of driving me nuts and the test code is very unreadable for me. So I’m going to remove the “training wheel” comments provided by the RubberDuck system when it created all the test module stubs.
Ok, that is done and the tests all still pass, so my refactoring did not affect any of the tests. If it had, I would have to correct it before moving on to create a new failing test. Here now is my code. It is an improvement, but I still think it could stand to be organized a little better, but I don’t feel like continuing to refactor at the moment. I’m not sure exactly what else to do. The comments still in it are actually used to control RubberDuck so I left those in. That would mess up the tests I think if I removed them. Anyway, here is the test module code now:
'@TestModule
'@Folder("Tests")
Option Compare Database
Option Explicit
Option Private Module
#Const LateBind = LateBindTests
#If LateBind Then
Private Assert As Object
Private Fakes As Object
#Else
Private Assert As Rubberduck.AssertClass
Private Fakes As Rubberduck.FakesProvider
#End If
Private NewForm As New Access.Form
Private FormListenerTest As FormListener
'@ModuleInitialize
Private Sub ModuleInitialize()
#If LateBind Then
Set Assert = CreateObject("Rubberduck.AssertClass")
Set Fakes = CreateObject("Rubberduck.FakesProvider")
#Else
Set Assert = New Rubberduck.AssertClass
Set Fakes = New Rubberduck.FakesProvider
#End If
DoCmd.OpenForm "TestForm"
Set NewForm = Forms("TestForm")
End Sub
'@ModuleCleanup
Private Sub ModuleCleanup()
Set Assert = Nothing
Set Fakes = Nothing
DoCmd.Close acForm, "TestForm"
Set NewForm = Nothing
End Sub
'@TestInitialize
Private Sub TestInitialize()
Set FormListenerTest = New FormListener
End Sub
'@TestCleanup
Private Sub TestCleanup()
Set FormListenerTest = Nothing
End Sub
'@TestMethod("FormListener")
Private Sub CreateFormListener()
On Error GoTo TestFail
Assert.Succeed
TestExit:
'@Ignore UnhandledOnErrorResumeNext
On Error Resume Next
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
Resume TestExit
End Sub
'@TestMethod("FormListener")
Private Sub SetupFormListener()
On Error GoTo TestFail
FormListenerTest.Setup NewForm
Assert.Succeed
TestExit:
'@Ignore UnhandledOnErrorResumeNext
On Error Resume Next
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
Resume TestExit
End Sub
'@TestMethod("FormListener")
Private Sub FormListenerHearsFormBeforeUpdateEvent()
On Error GoTo TestFail
FormListenerTest.Setup NewForm
NewForm.TestText = "TestingUpdate"
DoCmd.RunCommand acCmdSaveRecord
Assert.IsTrue FormListenerTest.BeforeUpdateTriggered
TestExit:
'@Ignore UnhandledOnErrorResumeNext
On Error Resume Next
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
Resume TestExit
End Sub
'@TestMethod("FormListener")
Private Sub FormListenerReturnsCountOfOneForTestTextField()
On Error GoTo TestFail
FormListenerTest.Setup NewForm
NewForm.TestText = "TestingUpdate"
DoCmd.RunCommand acCmdSaveRecord
Assert.AreEqual FormListenerTest.TimesFieldChanged("TestText"), CLng(1)
TestExit:
'@Ignore UnhandledOnErrorResumeNext
On Error Resume Next
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
Resume TestExit
End Sub