3

How does one unit test an object or collection - specifically a JSON packet?? Do you just test for he structure, key/values pairs? regardless of values??

Do I create an empty collection? and compare? same question applies to any complex object (query,structure,array, etc)

Any help by real examples would be greatly appreciated.

  • Im just getting started with TDD, and using MXUnit... Im not really quite sure where to begin... Here is a sample of one basic test,

    // Arrange 
        // obj set up - handled in "setup"
    
    // Act 
        var expect = 1;         
        var result = obj.getPersonByID();
        debug( result ); // throws data back to the tester for display
    
    // Assert 
        assertEqual( expect, result, 'Expected #expect#, got #result#.' );
    
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
j-p
  • 3,698
  • 9
  • 50
  • 93
  • What's important from your function, is it the contents of the JSON, or merely the fact you're getting back a JSON packet? If the former, could you use deserializeJSON() to convert it back to a CF struct, which will then be easier to test (e.g. checking all the expected keys are present) – duncan Feb 21 '14 at 09:38
  • 1
    The way I test for these is to NOT set `returnFormat` in your function definition and test it as you would any other code. If you need JSON for a particular item, simply add `returnFormat=json` to the query string if accessing the CFC method directly. If you are using a framework, you can build a process that generate the JSON for you. – Scott Stroz Feb 21 '14 at 13:03
  • RE: Duncan's comment -Would I write a separate test for EACH key? Which the little reading I've done, THAT seems to be the prescribed way... "one" assertion per test, and it's the path I'm leaning towards. please weigh in if there are arguments for/against. – j-p Feb 21 '14 at 19:55

1 Answers1

0

Use the following process:

  • Create a mock JSON
  • Deserialize JSON to a struct using DeserializeJSON
  • Create an array of keys using StructKeyArray
  • Loop through the array
  • Assert the name of each key

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265