Comments (3)
I would love this! But, as far as I can tell, it's just not possible with VBA (I'd love to be proven wrong).
I think your best bet for something like this is using "Dependency Injection" and/or Interfaces. You can setup your dependencies as Object
in your Class
or Module
and have them passed in by Workbook
at runtime and pass in mocks for testing. You can use interfaces to avoid generic using Objects
.
Example (haven't tested this, but something like it should work):
' Module.bas
' ---
Public Http As IWinHttpRequest
Public Function GetJson(Url As String) As String
Http.Open "GET", Url
Http.Send
GetJson = Http.ResponseText
End Function
' Workbook
' ---
Private Sub Workbook_Open
' Inject runtime dependencies
Set Module.Http = New WinHttpRequestWrapper
End Sub
' Specs_Module.bas
' ---
Public Function Specs As SpecSuite
Set Specs = New SpecSuite
With Specs.It("GetJson should call Open and Send")
' Initialize and inject test dependency
Dim Spy As New SpyWinHttpRequest
Set Spy.Actual = Module.Http
Set Module.Http = Spy
Module.GetJson "http://..."
.Expect(Spy.OpenCallCount).ToEqual 1
.Expect(Spy.SendCallCount).ToEqual 1
' Reset
Set Module.Http = Spy.Actual
Set Spy = Nothing
End With
End Function
' IWinHttpRequest.cls
' ---
Public ResponseText As String
Public Sub Open(Method As String, Url As String, _
Optional Async As Boolean = False)
End Sub
Public Sub Send(Optional Body As String = "")
End Sub
' WinHttpRequestWrapper.cls
' ---
Implements IWinHttpRequest
Private pHttp As WinHttpRequest
Private Sub IWinHttpRequest_Open(Method As String, Url As String, _
Optional Async As Boolean = False)
pHttp.Open Method, Url, Async
End Sub
Private Sub IWinHttpRequest_Send(Optional Body As String = "")
pHttp.Send Body
End Sub
' ... Get/Let ResponseText
Private Sub Class_Initialize()
Set pHttp = New WinHttpRequest
End Sub
' MockWinHttpRequest.cls
' ---
Implements IWinHttpRequest
Private Sub IWinHttpRequest_Open(Method As String, Url As String, _
Optional Async As Boolean = False)
' Mock...
End Sub
Private Sub IWinHttpRequest_Send(Optional Body As String = "")
' Mock...
End Sub
' ... Get/Let ResponseText
' SpyWinHttpRequest.cls
' ---
Implements IWinHttpRequest
Public Actual As IWinHttpRequest
Public OpenCallCount As Long
Public SendCallCount As Long
Private Sub IWinHttpRequest_Open(Method As String, Url As String, _
Optional Async As Boolean = False)
OpenCallCount = OpenCallCount + 1
Actual.Open Method, Url, Async
End Sub
Private Sub IWinHttpRequest_Send(Optional Body As String = "")
SendCallCount = SendCallCount + 1
Actual.Send Body
End Sub
' ... Get/Let ResponseText
from vba-test.
wow thanks. I will close this as it looks outside of scope of excel-tdd
from vba-test.
This is a two year old issue, but I wanted to comment in case anyone else was looking at mocking in VBA/VB6:
There is a project called VBMock which helps with creating mock classes, and a helper project called Mock Objector Generator for VBMock to help automate the generation of VBMock objects. They're both VB6-targeted, but can be used in VBA.
Rubberduck-VBA recently added a Fakes system using EasyHook to overwrite the function (within a DLL) being called at a specific ProcAddress
. Both of those projects are .NET
-based, but the general process applies. It's a little bit more dangerous to do directly from VBA/VB6, but should be possible.
Unfortunately, I think both of the above solutions may be out-of-scope for VBA-TDD.
The safest solution is what @timhall provided an example of: Use Interfaces and pass in Fake objects that monitor usage. It's unfortunately a very manual process but better than nothing.
-robodude666
from vba-test.
Related Issues (20)
- Add IsTrue() IsNotTrue() IsFalse() IsNotFalse() HOT 2
- Need more explanatory examples HOT 4
- IsEqual does not support Array Types HOT 2
- SpecDefinition should return its own reference HOT 2
- Provide a 'toBeCloseTo' matcher for precision math comparison HOT 3
- Asserting Err Object HOT 1
- Fail suite for unhandled error
- Run Test Suite classes with method per test HOT 6
- TestFixture() class as part of block?
- WorkbookReporter should be able to create it's own output sheet
- ArrayIncludes() cannot handle multi-dimension arrays HOT 1
- Function Examples end as Sub HOT 2
- Which documentation generator do you use?
- Possible memory leak?
- VBA testing HOT 1
- Update Workbook proxy HOT 1
- Plan: Update runners HOT 2
- Plan: Performance testing extension HOT 4
- AfterEach HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vba-test.