taffy, di1 and testbox

Hi Guys,

I’m trying to implement testbox on a taffy project with DI1 dependency injection , But I’m getting and error when calling a method that calls another via the dependency injection.

can’t call method [ObjectName] on object, object is null.

Please Help.

You have to mock that dependency. When you create the CFC in your test case, you’re bypassing DI/1.

Hi Brad , Thanks for your answer , I mocked the properties but still getting the same error, How I mocked the DI?/1 ioc framework ?

The error was that the exception service didn’t exist. Did you mock that too? If not, I would expect it to not work :slight_smile:

Why you expected that ? that was sarcastic right ? 😂😂😂

It works when I mocked the exception service, check, I don’t know if it a good practice for testing…

Normally I would not mock the system under test (pointsService), unless I wanted to verify interactions. You are right that you don’t need to create the actual dependency classes for testing, but I would expect that you set a mock in their place instead of actual objects.

In line 28, I would expect a stubbed object to be set instead of the real ones.
In line 29, I would expect a mock for the exception service to be set.

Hi Royce, thanks for your suggestion , I’m not sure what you mean whan say I would not mock the system under test (pointsService). Here the final test. I appreciate your opinion and feedback.

Thanks.

Normally I would not mock the system under test (pointsService),

Technically, it’s not mocking the PointsService. createmock() creates the CFC and leaves the methods intact but also adds in the special methods like $() to allow you to inject the mocked bits.

In line 28, I would expect a stubbed object to be set instead of the real ones.
In line 29, I would expect a mock for the exception service to be set.

This is correct. Unit tests are meant to run only the function you want to test (SUT) in a vacuum with all other dependencies faked. What needs to be done is to createEmptyMock() of the pointsDAO and exceptionservice and THEN add the mocked methods that are needed to those objects. Something like

var pointsDAO = createEmtpyMock( ‘models.services.points.PointsService’ ).$( ‘getLoyaltyPoints’, querySim( … ) )
var exceptionService = createEmtpyMock( ‘…exceptionService’ ).$( ‘handleException’ )

Also, stop putting stuff in the application scope. Just create the SUT locally, inject all the mocked dependencies directly, and then call the method you need to test. The idea is that you are not wanting to run or test any code outside of the getPoints() method. You also don’t need to set up the application.env stuff. Your not even using your real DAO in these tests so it doesn’t even need to be operational. You’re trying to test more than one method in your unit test.

Unit testing can be very tedious as you can see. This is honestly where frameworks like Coldbox have it much easier with the Integration testing that is supported where you have a mechanism to test an entire framework event.

Thanks a lot guys , I’ll do the appropiate changes today.