62930

pact: validate that an array contains a matching hash

Question:

I have a service that returns an array of hashes, the order of which is non-deterministic. I need to validate that there exists one hash that has a certain key/value, and that hash is populated with data, but the rest of the hashes I don't care about their data. For example, if the service returns this:

[ { "key":"meaningless1", "data": { } }, { "key":"meaningless2", "data": { "some": "data", "goes": ["here"] } }, { "key":"meaningful", "data": { "regex": "value", "integer": 1, "boolean": true } } ]

I want to validate that in the array, there is a hash that has "key":"meaningful", and that the data hash in that hash has a key of regex with a value that matches a regex, a key of integer that is an integer, and a key of boolean that is a boolean. I don't care if the other hashes have data at all, or if the data they have matches this schema.

I can't use EachLike, because that will verify the schema against all the hashes, not just the one with the right key. I also tried something like this:

expected = [ { "key":"meaningful", "data":{ "regex":Term("v.*", "value"), "integer":Like(1), "boolean":Like(True) } } ]

But it simply tried to verify that against the 0th element in the resulting array, which means it tried to verify against:

{ "key":"meaningless1", "data": { } },

Is what I want possible?

Answer1:

This is not currently possible. You might like to join this conversation about the feature that you are asking for: <a href="https://github.com/pact-foundation/pact-specification/issues/38" rel="nofollow">https://github.com/pact-foundation/pact-specification/issues/38</a>

However, one thing that I will point out is that pact works best when you can control the data on the provider. If you cannot control the data, then pact might not be the best tool for your scenario. Have a read of: <a href="https://github.com/pact-foundation/pact-ruby/wiki/Why-Pact-may-not-be-the-best-tool-for-testing-public-APIs" rel="nofollow">https://github.com/pact-foundation/pact-ruby/wiki/Why-Pact-may-not-be-the-best-tool-for-testing-public-APIs</a>

Recommend

  • Controling the Volume of an Audio Clip in Java 1.4
  • Choose one key arbitrarily in a dictionary without iteration [duplicate]
  • pandas parse csv with left and right quote chars
  • Fraction length
  • How to capture enclosing scope in Runnable
  • Convert unix time to week day
  • Oracle ListaGG, Top 3 most frequent values, given in one column, grouped by ID
  • Rails AREL .where statement
  • Why I can't use uniform1f instead of uniform4f for setting a vec4 uniform?
  • Is there any way to use wpdb prepare statements for array implode(' OR ', $myArray)?
  • Regex for Specific Tag
  • Efficient User-Agent Regex to find Safari in Python
  • Is there a way to set up a fallback for the formAction attribute in HTML5?
  • Vuejs: Lifecycle hooks of child routerview components using keep alive
  • Facebook iOS SDK Not Calling Completion Handler
  • Aptana 3 remove bundle (jquery)
  • CXF JAXB JAXBEncoderDecoder unmarshalling error : unexpected element when having qualified elements
  • Trying to get the char code of ENTER key
  • Execute scripts AJAX returns
  • Z3: Convert between FP and BitVector?
  • MySQL Order by column = x, column asc?
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • PHP buffered output depending on server setting?
  • Functions in global context
  • Email verification using google app script and google forms
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Regex thinks I'm nesting, but I'm not
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • Is possible to count alias result on mysql
  • Check if a string to interpolate provides expected placeholders
  • Convert array of 8 bytes to signed long in C++
  • RestKit - RKRequestDelegate does not exist
  • Bitwise OR returns boolean when one of operands is nil
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?