Dependency Variable Doesnt Exist

202 views Asked by At

Newbie in coldbox so please have patience with me.

I am trying to implement TDD on my coldbox application.

Under my service model I inject this dependency.

property name="wirebox" inject="wirebox" property name="populator" inject="wirebox:populator";

On my service model I have this method. GetallUsers()

User function new(){
    return wirebox.getInstance("User");
}

function getAllUsers(){
    var users= queryExecute(
        "SELECT * FROM USERS",
        {},
        {returnType="array"}
    ).map(function(user){
        return populator.populateFromStruct(new(),user);
    });
    return users;
}

And on my UserServiceTest I have this code:

component extends="coldbox.system.testing.BaseModelTest" model="models.UserService"{





/*********************************** 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( "Users Suite", function(){

            it( "can get list of users", function(){
                

                var stubPopulator = stub().$( 'populateFromStruct', {} );
                model.$property( 'populator', 'variables', stubPopulator );
                var users= model.getAll();

                expect( event.getPrivateValue( "users") ).toBeStruct();
            
            });
        });
    }

But I got this error saying **variable [POPULATOR] doesn't exist**.

Hoping someone can help me.

1

There are 1 answers

5
Brad Wood On

You didn't show the full test bundle, but based on the name it would appear it is a unit test (or a ColdBox model test, which is a type of unit test). Unit tests do not spin up the ColdBox framework by default and do not process injections for the CFCs under test. They are created "naked" and it's up to you to provide mocks for an dependencies that CFC has.

So in this case, you'd need to provide a mock populator to your model to be used for the test. So something like this:

var stubPopulator = createStub().$( 'populateFromStruct', {} )
model.$property( 'populator', 'variables', stubPopulator )
var users= model.getAll();

My stubed populator just returns an empty struct. It's also worth noting I don't think your queryMap() is returning a struct like you think it is so you may need to confirm the functionality of that method.

Alternatively, you could switch to more of an integration test where you set this.loadColdBox to true in your test CFC's pseduo-constructor and then use getInstance( 'UserService' ) to get a fully built instance of your UserService which would have the populator injected into it. Exactly how this would look like depends on several things you haven't shared such as your test harness setup, and your test bundle CFC's base class.