[Testbox 4.2.0] How Can I Instantiate Modules in Unit Tests?

38 views
Skip to first unread message

David Levin

unread,
Dec 20, 2020, 3:19:03 PM12/20/20
to ColdBox Platform
Happy Holidays CFers. :)

I'm building out some unit tests for a model, and I'd like to be able to leverage CBValidation to make sure the model will pass validation checks when the proper data is added and will fail when something is missing.

However, I'm not sure how to inject CBValidation, which is already installed and in use in the main app.  When I try calling:
var validationManager = application.wirebox.getInstance( "ValidationManager@cbvalidation" );

I get the error, "Requested instance not found: 'ValidationManager@cbvalidation'"

Here's what my unit test component looks like:
component extends="coldbox.system.testing.BaseModelTest" model="models.quote.Quote" {
    
    variables.validData = {
       // ... test data
    };
    
    /*********************************** LIFE CYCLE Methods ***********************************/

    function beforeAll(){
        super.beforeAll();
        
        // setup the model
        super.setup();
        
        // init the model object
        model.init();

    }

    function afterAll(){
        super.afterAll();
    }

    /*********************************** BDD SUITES ***********************************/
    
    function run(){

        describe( "A Quote", function(){
            
            it( "can be created", function(){
                expect( model ).toBeComponent();
            });

            it( "will validate when given proper data", function() {

                var validationManager = application.wirebox.getInstance( "ValidationManager@cbvalidation" );
                
                var quote = application.wirebox
                    .getObjectPopulator()
                    .populateFromStruct(
                        target = model,
                        memento = validData
                    )
                ;

                var validationResult = validationManager.validate( quote );

                debug( validationResult );

            } );
        
        });

    }

}

Any help or insight that you share would be very helpful!


Reply all
Reply to author
Forward
0 new messages