BlueReddington
BlueReddington

Reputation: 79

Cypress assert API response with value that is generated in the API

I want to assert that my api response contains one parameter that is generated in the api like an ID. I am aware that i can't predict the value but it's possible to validate the type of the value?

For example my response payload is the following:

{ json: "request" id: "TX8982XASA" }

I want to assert the ID value.

To assert a return payload i use the following:

expect(response).to.have.property(key, value)

The response is the object that is returned from the API and the key = ID. In this case the value is "TX8982XASA" but since it's always generated i can't use it directly.

Upvotes: 0

Views: 918

Answers (1)

Constance
Constance

Reputation: 547

Two assertions seem appropriate to run in this instance - existence and typeof.

// property exists
expect(response).to.have.property(key)
// or
expect(response).to.have.property('id')

// property has type
expect(typeof response[key]).to.eq('string')  // whatever type it is
// or
expect(typeof response.id).to.eq('number')

Upvotes: 3

Related Questions