Writing a VBA Unit Testing Suite -
i trying write addin let me write unit tests vba. have class setup testing assertions etc. missing way stubing/mocking classes there way of doing that?
for clarification talking instantiating objects pretending of different class. example, want write mock of address class person class work with:
person class
private home address sub changeaddress(newstreetaddress string) address.street = newstreetaddress end sub
address class
dim street string
testable, object-oriented vba code lets control dependencies, must written against abstractions. extensible logging on code review stack exchange example of that.
in nutshell, code needs depend on interfaces - can unit tests pass in "fake" implementations. take ilogger
class module linked cr post:
option explicit public sub log(byval output string) end sub public property name() string end property public property minlevel() loglevel end property
that's interface. 1 implementation called debuglogger
, goes - notice implements ilogger:
option explicit private type tdebuglogger name string minlevel loglevel end type private tdebuglogger implements ilogger public function create(byval loggername string, byval loggerminlevel loglevel) ilogger dim result new debuglogger result.name = loggername result.minlevel = loggerminlevel set create = result end function friend property name() string name = this.name end property friend property let name(byval value string) this.name = value end property friend property minlevel() loglevel minlevel = this.minlevel end property friend property let minlevel(byval value loglevel) this.minlevel = value end property private sub ilogger_log(byval output string) debug.print output end sub private property ilogger_minlevel() loglevel ilogger_minlevel = this.minlevel end property private property ilogger_name() string ilogger_name = this.name end property
now if have code uses ilogger
, unit tests can pass in implementation that, instead of printing stuff console, raises event reports message being logged , @ level: test module can handle these events, set module-level flag, , [assuming have implemented assert
class, or using rubberduck unit tests] can call assert.istrue
validate flag. or whatever.
bottom line, can totally done: have irepository
interface implementations hitting database, , unit tests use "fake" implementations wrap collection
instead.
as mocking, in, generating implementation on-the-fly, , injecting that, ... that's not done, can see rubberduck offering in future version.
disclaimer - co-own rubberduck project , repository.
Comments
Post a Comment