【问题标题】:Testing an array of structures in mxunit在 mxunit 中测试结构数组
【发布时间】:2011-09-23 13:59:11
【问题描述】:

在 mxunit 中测试返回结构体数组的函数的最佳方法是什么?现在我正在做这样的事情:

var actual = variables.pbj.getFunctions();  //returns [{name="getAccountNumber", value="0"},{name="getAccountName", value=""}]
var found = false;

//look for get account number
for(var i = 1; i lte arrayLen(actual); i ++){
    if(structKeyExists(actual[i],"name") && actual[i].name eq "getAccountNumber"){
        found = true;
        break;
    }
}

if(NOT found){
    fail("Struct key getAccountNumber didn't exist");
}

    found = false;

//look for account name
for(var i = 1;i lte arrayLen(actual); i ++){
    if(structKeyExists(actual[i],"name") && actual[i].name eq "getAccountName"){
        found = true;
        break;
    }
}

if(NOT found){
    fail("Struct key getAccountName didn't exist");
}

这感觉有点笨拙和脆弱。有人知道更好的方法吗?

【问题讨论】:

    标签: coldfusion mxunit


    【解决方案1】:

    这就是我会做的:

    var actual = variables.pbj.getFunctions();  //returns [{name="getAccountNumber", value="0"},{name="getAccountName", value=""}]
    
    for (thisStruct in actual) {
        if(NOT structKeyExists(thisStruct,"name") || thisStruct.name neq "getAccountNumber"){
            fail("Struct key getAccountNumber didn't exist");
        }
        if(NOT structKeyExists(thisStruct,"name") || thisStruct.name neq "getAccountName"){
            fail("Struct key getAccountName didn't exist");
        }
    }
    

    【讨论】:

    • 虽然,现在我想起来了,你的测试总是会失败,因为 name 不能同时是两个值。不过,我想你会大致了解我的代码。
    • 不要忘记我正在返回一个结构数组。更复杂的是,数组可以按任何特定顺序排列,因为它是从对象元数据生成的。
    • 我在这里可能考虑的唯一变化不是简单地在第一件事出错时立即失败,而是在遇到问题时将失败消息附加到failuresList 变量(例如:“数组元素#i# 中缺少getAccountNumber”)。一旦我检查了整个事情,检查failuresList的长度,如果它不是零长度,则失败(failuresList)。有时,得到一堆反馈是很方便的,而不是在第一次失败时停下来。不过,这取决于具体情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2012-02-18
    • 2017-08-06
    相关资源
    最近更新 更多