1

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.

Undecided Dev
  • 840
  • 6
  • 16
  • I have answered below, but I would also like to invite you to our Ortus Community Forum (we're the makers of TestBox and ColdBox) where we can have better back-and-forth conversation about what you're doing. https://community.ortussolutions.com/ – Brad Wood Jul 12 '22 at 22:40

1 Answers1

2

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.

Brad Wood
  • 3,863
  • 16
  • 23
  • Hello Brad. That is exactly what I'm trying to achieve. As I tried your snippet given. Unfortunately Im still getting error. Saying stub is not a function. Is there something I am missing. – Undecided Dev Jul 12 '22 at 23:45
  • I make some changes on my post. please check. – Undecided Dev Jul 12 '22 at 23:47
  • Oops, just a typo. Should be `createStub()`. Please refer to the TestBox documentation: https://testbox.ortusbooks.com/mocking/mockbox/creating-a-stub-object – Brad Wood Jul 13 '22 at 00:49
  • Thank you Brad. I tried once again and relay on the link. Now I only got this error "component [testbox.system.mockutils.Stub] has no function with name [getInstance]". I have function new() to populate the component. See my latest edit on my post. – Undecided Dev Jul 13 '22 at 00:54
  • Well yes, you need to mock EVERYTHING. This is the nature of unit testing. Until you've provided mocks for all the external bits of logic, you won't be able to test the function in question. – Brad Wood Jul 13 '22 at 16:08