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.

iBug
  • 35,554
  • 7
  • 89
  • 134
trial999
  • 1,646
  • 6
  • 21
  • 36

1 Answers1

9

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.

Dave McNulla
  • 2,006
  • 16
  • 23
  • Nice and thanks! looks like this will solve my problem. – trial999 Nov 30 '15 at 23:09
  • But what if I want to verify json together with other parameters, for example: Then the following response is sent to a | user | json | | user1 | {"user1": {"dddd":"dddd","ggggg":"ggggg"}}| | user2 | {"user2": {"dddd":"dddd","ggggg":"ggggg"}}| – rideronthestorm Sep 27 '17 at 10:45
  • 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" – Dave McNulla Sep 29 '17 at 04:03
  • what is this? `expect(rest_stub.body).to eq(message)` – likejudo May 19 '20 at 11:29
  • [rspect docs for expect(something).to eq(something else)](https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers) and the rest_stub.body would be the expected response to the call – Dave McNulla May 20 '20 at 23:31