6

I have a need to represent JSON object in the feature file. I could use a json file for this for the code to pick up. But this would mean that i cant pass values from the feature file.

Scenario: Test

Given a condition is met

Then the following json response is sent
 | json |
 | {"dddd":"dddd","ggggg":"ggggg"}|

Above works for a normal json. However if there are nested objects etc then writing the json in a single line like above would make the feature very difficult to read and difficult to fix.

Please let me know.

1 Answer 1

10

You can use a string to do that, it makes the json much more legible.

Then the following json response is sent
  """
   {
      'dddd': 'dddd',
      'ggggg': 'ggggg',
      'somethingelse': {
        'thing': 'thingvalue',
        'thing2': 'thing2value'
      }
    }
  """

In the code, you can use it directly:

Then(/^the following json response is sent$/) do |message|
  expect(rest_stub.body).to eq(message)
end

or something like that.

Sign up to request clarification or add additional context in comments.

5 Comments

Nice and thanks! looks like this will solve my problem.
But what if I want to verify json together with other parameters, for example: Then the following <json> response is sent to a <user> | user | json | | user1 | {"user1": {"dddd":"dddd","ggggg":"ggggg"}}| | user2 | {"user2": {"dddd":"dddd","ggggg":"ggggg"}}|
That's a different question. These are simple questions that can easily be found by searching. I saw 3 good answers at the top when I search google "cucumber json"
what is this? expect(rest_stub.body).to eq(message)
rspect docs for expect(something).to eq(something else) and the rest_stub.body would be the expected response to the call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.