2

I see in this answer that for Java you can set the visibility of a private method to "true" in a unit test in order to test the method. Is there something like this available for VBA, so that I can unit test private methods using RD-VBA?

If not, and I have a class that works out some logic in three private methods and give it back to a return value, am I doomed to only give a input value and test the return value, without being able to test the three private methods doing the heave lifting in between?

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Alfa Bravo
  • 1,961
  • 2
  • 25
  • 45
  • 1
    I know you can execute private methods residing in normal modules via the `Application.OnTime` command. I'm not sure if that'd work for class modules - or if that would be a sensible thing to do. For reasons why you should not do what you intend to do, watch *[TDD - where did it all go wrong?](https://www.youtube.com/watch?v=EZ05e7EMOLM)* by Ian Cooper which I found highly enlightening and agreeable. – Inarion Apr 12 '18 at 14:57
  • @Inario, I forgot to leave a comment to say what a great recommendation your video was. I loved every minute of it and it did indeed enlighten me a lot about the whole subject. Thanks for sharing! – Alfa Bravo Apr 17 '18 at 04:55

2 Answers2

8

You shouldn't need to write tests for private methods, regardless of the language. You test your public API, what's private is implementation detail that isn't relevant.

If it is relevant and important enough to be tested on its own, then you should extract that private method to another class, and expose it as a public member of that class.

For example once I had a form and I wanted to limit user input in a textbox to numeric characters, and since I'm reusing that logic elsewhere then instead of treating it as an implementation detail of my form, I extracted a AsciiInputValidator class, and its public IsValidNumericValue method could be tested in every possible way as its own SUT.

Don't test private methods: the public ones invoke them anyway.

Unfortunately the Extract Class refactoring feature is not implemented as of this writing, so for now Rubberduck can't do this automatically for you... but it's definitely in-scope and if you're reading this and you're up for a bit of a C# metaprogramming challenge, go for it, pull requests are always welcome!

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
0

Can you add a public wrapper like

public sub testPrivateSub(param1,param2...)
     PrivateSub(param1,param2....)
end sub

private sub PrivateSub(param1,param2....)
....
end sub
Nathan_Sav
  • 8,466
  • 2
  • 13
  • 20