1

What is the best way to test a function that returns an array of structres in mxunit? Right now i'm doing something like this:

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");
}

This feels somewhat kludgy and fragile. Anybody know of a better way?

bittersweetryan
  • 3,383
  • 5
  • 28
  • 42

1 Answers1

2

This is what I would do:

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");
    }
}
  • Although, now that I think about it, your test was always going to fail, since name can't be both values. I think you'll get the general idea of my code, though. – Shannon Hicks Sep 23 '11 at 14:35
  • Don't forget that i'm returning an Array of structures. To complicate things more that array can be in any particular order since its being generated from an objects metadata. – bittersweetryan Sep 23 '11 at 14:36
  • The only variation I *might* consider here is to not simply fail-out as soon as the first thing is wrong, but whenever a problem is encountered, append a failure message to a failuresList variable (eg: "getAccountNumber missing from array element #i#"). Once I've checked the whole thing out, check the length of failuresList, and if it's not zero-length do fail(failuresList). Sometimes it's handy to get a bunch of feedback back, rather than just halt on the first failure. It depends on the circumstances though. – Adam Cameron Sep 23 '11 at 14:40