Is there a way to mock a private method in TestBox...
# testing
b
Is there a way to mock a private method in TestBox? I am testing a public method that calls a private method doing stuff I don't want it to do during a test. I just want to mock the private method being called by the sut to return a simple string. My understanding is that you can't mock private methods in the way I'm doing currently with prepare mock and
.$("myprivatemethod", "return this")
a
I'm pretty sure you can mock private methods like that. If not you can always make the method public first using
makePublic( sut, "myprivatemethod" )
b
I tried that, but for some reason get a makePublic function not found error (using Lucee v5 and TestBox v5). I just tried making the function public in the source code and even that didn't work and it's still being called with actual implementation.
Copy code
myComp = new myComp();
myComp = mockBox.prepareMock(myComp);
myComp.$("methodName", "return this string");
That just doesn't work even with the method made public. Am I doing this wrong?
a
can you post the whole testcase?
You should just be able to do:
Copy code
var model = prepareMock( new myComp() );
model.$( "myMethod", "return me" );
expect( model.myMethod() ).toBe( "return me" );
b
I'll try copying he way you're doing it, but the test case is calling a public method, that calls a private method in it, so that's the most important difference I can think of (in your example the assertion is directly on the method result, but I'm just trying to get an inner function to do nothing during the test and the surrounding functionality in the parent method (a public method on
myComp
that calls a private method in it) is what I'm interested in testing)
a
Just tried it and it works as expected.
👀 1
File under test
Untitled
testcase
Untitled
Is that what you are trying to do?
b
Yes that's the idea, only I don't care about the return value of the private method in the assertion, only the public method return val which returns something else not related to the private function (the private function is a decryption method I want to just return a dummy string which is not important in the test it just makes the test more complicated to arrange without mocking it). Thank you for fleshing this out. I'll review what I might be doing wrong, but what you have looks pretty similar to what I'm doing, obviously I'm missing something. If I have time later I'll try to make a reproduction of my test for more context. Thanks again for taking a look.
👍 1