In Testbox, for integration testing, is it possibl...
# testing
j
In Testbox, for integration testing, is it possible to mock objects that are called from a handler prior to the
execute
call of the handler?
a
I have to say... despite using TestBox since it had a
0
as a major version I don't know how to interpret "handler" or the execution thereof?
Is this TestBox jargon, or some jargon from [whatever it is yer testing]? Maybe some code would help..?
j
Hey Adam! A handler is a controller in Coldbox MVC land. Give me a bit and I'll write up something more detailed.
a
Ah
Right so it's a "CB integration with TB" question. I won't be able to help you. I know f-all about CB
j
Ok, first of all, I have a pretty good grasp of mocking for unit tests. So I understand mocking in that context. A typical unit test I'm running extends
coldbox.system.testing.BaseModelTest
. Then in
beforeAll()
I do something like
calculatorService = createMock( "models.CalculatorService" );
for the model under test. Then
calculatorData = createEmptyMock( "models.CalculatorData" );
which is where a sql query resides and I, of course, don't want to hit the actual database in a unit test. To mock the DI of the calculatorData into the service
calculatorService.$property( propertyName="calculatorData", mock=calculatorData );
Then in the test itself I can use
querySim()
to simulate a query and use that to mock the method in the data layer
calculatorData.$(  "getData", querySimData );
Then I simply run the method in the calculatorService that I want to test that calls the mocked data layer. All this works great. However, now I want to do something similar in a Coldbox handler, which is typically where an integration test would be used (or so I understand). I've had some success with handlers that have form data passed in. I've been doing something like
getRequestContext().setValue( "employeeName", "Joe Chastain" );
which sets the value in the rc scope for a variable named
employeeName
. Then
var event = execute( event="main.calculate", renderResults=true );
to execute the handler. Then I can check the rendered results for various things. Again, this works great. But now I want to be able to mock objects that the handler calls. This is different from the unit test because instead of creating a mock object to test, the
execute
method is called. I've not yet been able to figure out how to set the mocked object as a property of the handler because of this difference.
1
Right so it's a "CB integration with TB" question.
I won't be able to help you. I know f-all about CB
Understood. Thanks for chiming in anyway. 🙂
I tried mocking the handler just for fun, as if it were a model in a unit test, but then I have a blank event object argument for the handler which breaks the test.
a
Being context neutral, I presume CB keeps all its dependencies in a WB container? And I presume the integration test approach uses an loaded app with its DI container etc? Can you pull the handler out of the container and mock it?
BTW: pleased to see someone who's actually comfortable with testing and mocking and stuff in the CFML community.
👍 1
(presume you can at least read enough PHP to follow along those methods)
j
Good thoughts and thanks for the link. I'll take a look and play around with it some more.
BTW: pleased to see someone who's actually comfortable with testing and mocking and stuff in the CFML community.
Thanks. I'm just getting started and know I have a ton more to learn. I'm pretty sure I'll be going to Into the Box in May and attending Brad's full day 'TestBox' workshop.
a
That sounds like gold. Sorta thing I'd like to attend, but... my CFML indulgence this year might be CFCamp.
j
Nice! I've never been. I was at the CF Summit in Vegas pre-pandemic but haven't attended a conference in-person since. Really quite excited for it — especially that workshop since right now testing is the bee in my programming bonnet.
1
a
I enjoy the corridors &... erm... the bar... more than I do the presentations. But that workshop sounds good.
😆 1