简体   繁体   English

依赖变量不存在

[英]Dependency Variable Doesnt Exist

Newbie in coldbox so please have patience with me.冷箱中的新手,所以请耐心等待我。

I am trying to implement TDD on my coldbox application.我正在尝试在我的冷箱应用程序上实现TDD

Under my service model I inject this dependency.在我的服务模型下,我注入了这种依赖。

property name="wirebox" inject="wirebox" property name="populator" inject="wirebox:populator"; 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:在我的 UserServiceTest 我有这个代码:

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** .但是我收到这个错误,说**variable [POPULATOR] doesn't exist**

Hoping someone can help me.希望有人可以帮助我。

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).您没有显示完整的测试包,但根据名称显示它是一个单元测试(或 ColdBox 模型测试,它是一种单元测试)。 Unit tests do not spin up the ColdBox framework by default and do not process injections for the CFCs under test.默认情况下,单元测试不启动 ColdBox 框架,也不处理被测 CFC 的注入。 They are created "naked" and it's up to you to provide mocks for an dependencies that CFC has.它们是“裸”创建的,您可以为 CFC 拥有的依赖项提供模拟。

So in this case, you'd need to provide a mock populator to your model to be used for the test.因此,在这种情况下,您需要为模型提供一个模拟populator器以用于测试。 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.还值得注意的是,我认为您的 queryMap() 不会像您认为的那样返回一个结构,因此您可能需要确认该方法的功能。

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.或者,您可以切换到更多的集成测试,在测试 CFC 的伪构造函数中将this.loadColdBox设置为 true,然后使用getInstance( 'UserService' )获取UserService的完整构建实例,该实例将注入填充器进去。 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.这究竟会是什么样子取决于您尚未共享的几件事,例如您的测试工具设置,以及您的测试包 CFC 的基类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM