0

I want to test an external DTO that changes frequently.

ex:

I has a below javascript(json) file.

// javascript
const type User = {
    id: Number,
    name: String
}

// json
user: {
   id: Number,
   name: String,
}

At this time, the external API response changed.

// ex1) response (Add)
{
    id: Number,
    name: String,
    age: Number
}

// ex2) response (remove)
{
    id: Number
}

// and so on.

I want to get a failure result and change javascript file.

In this case, How do I test external DTO?

In other words, I hope to check for changed properties.

Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44
Doho Kim
  • 3
  • 1
  • Create different test cases for `Add` and `Remove` operations and assert structure of the different responses. What stops you? – Lin Du Aug 09 '21 at 08:13

1 Answers1

0

So let's assume you have an api response and an User object/class

const apiResponse = '{ "id": 1, "name": "someName", "missingProperty": 1 }';
const response = JSON.parse(apiResponse);

const User = {
    id: Number,
    name: String,
    newProperty: Boolean,
}

This validation is for missing properties on response

for(const key in User) {
    if(!(key in response)) {
        console.error(`Missing property from response ${key}`);
    }
}

This validation is for missing properties on User model

for(const key in response) {
    if(key in User) {
        if(response[key].constructor === User[key]) {
            continue;
        }
        console.error(`${key} is not proper type, ${response[key].constructor} instead of ${User[key]}`);
    }
    console.error(`Missing property from model ${key}`);
}
Beriu
  • 59
  • 7