Hi Marcus,
Tell me what do you see when you run the TypeMock Tracer? is the method being mocked? called?
I haven't used the "TypeMock tracer". In the code I:
* Create a mockobject for BusinessService
* Configure it with two AlwaysReturn settings to return a string each
* Cast a reference to the mockobjacts object
* Call one of the AlwaysReturn methods, expecting the string I just set a couple of lines up. <-- this fails every other time
What is the type of BusinessObject (abstract/ interface)?
BusinessService is an abstract class.
If you print out the type of businessService what do you get?
Here are new asserts in the code that pass showing you typenames:
Private Sub SetupBsMock()
SyncLock (Me)
If _businessMock Is Nothing Or _businessService Is Nothing Then
_businessMock = MockManager.MockObject(GetType(BusinessService))
_businessMock.Strict = True
_businessMock.StrictStatic = True
Debug.Assert(Not _CName Is Nothing, "CName should be set when mock is created")
Debug.Assert(_CName <> "", "CName should be set when mock is created")
_businessMock.AlwaysReturn("GetCName", _CName)
_businessMock.AlwaysReturn("GetServiceName", _serviceName)
_businessMock.ExpectConstructor()
'_businessService = New TestBs
_businessService = CType(_businessMock.Object, BusinessService)
Debug.Assert(_businessService.GetType.FullName = "MockBusinessService")
Debug.Assert(_businessMock.GetType.FullName = "TypeMock.MockObject")
' This fails every other time:
Debug.Assert(_businessService.GetCName() = _CName, "Service should return name as setup")
End If
End SyncLock
End Sub
So I just discovered that if I do this exact same thing in the testclass (not part of the harnessclass) it starts to work. The following is a method that uses only local variables but creates a MockObject of the same class.
Although it is completely independent of any other code (referencewise) it pushes out the problem above that manifested itself in 20+ or so tests. However, it does not do so reliably. The previous problem appears pretty often, but it does affect the problem.
<Test()> _
Public Sub TTest()
Dim CName As String = "testEbc"
Dim serviceName As String = "testService"
Dim businessMock As MockObject
Dim businessService As BusinessService
SyncLock (Me)
If businessMock Is Nothing Or businessService Is Nothing Then
businessMock = MockManager.MockObject(GetType(BusinessService))
businessMock.Strict = True
businessMock.StrictStatic = True
businessMock.AlwaysReturn("GetCName", CName)
businessMock.AlwaysReturn("GetServiceName", serviceName)
businessMock.ExpectConstructor()
businessService = CType(businessMock.Object, BusinessService)
Debug.Assert(businessService.GetCName() = CName, "Service should return name as setup")
End If
End SyncLock
End Sub
(The other downside is that I seem to get the magic TypeInitializer exception more often.)
This is pretty scary, but I have to get it to work, somehow.