examples of unit testing methods that are expected to return JSON

1.3k Views Asked by At

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#.' );
    
1

There are 1 best solutions below

0
On

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