collectiveidea / json_spec

Easily handle JSON in RSpec and Cucumber
rubygems.org/gems/json_spec
MIT License
919 stars 114 forks source link

include matcher do not work with strings #31

Closed k-solutions closed 12 years ago

k-solutions commented 12 years ago

When deailng with having simple string to find in JSON it is not working properly.

See the case statement in match? method

laserlemon commented 12 years ago

Could you please give a code example?

k-solutions commented 12 years ago

Using the default profile... Feature: Meetings API

Background: # features/meetings/api.feature:2 Given I am a valid API user # features/step_definitions/api_steps.rb:46 And the following projects exist: # features/step_definitions/project_steps.rb:6 | id | title | | 1 | Steve Project | | 2 | Catie Project | And the following meetings exist: # features/step_definitions/meeting_steps.rb:2 | id | title | | 1 | Steve Meeting | | 2 | Catie Meeting |

@todo Scenario: Post action # features/meetings/api.feature:31 When I send a POST request to "/meetings.json" with the following: # features/step_definitions/api_steps.rb:61

actial response is : {"id":3,"start_time":"2015-09-27T18:30:00Z","title":"Steve Richert meeting","details":[{"description":null,"duration":60,"id":1,"project_id":1},{"description":null,"duration":120,"id":2,"project_id":2}]} """ { "meeting": { "title" : "Steve Richert meeting", "start_time" : "2015-09-27 18:30", "details_attributes" : [ { "project_id" : 1, "duration" : 60 }, { "project_id" : 2, "duration" : 120 } ] } } """ /home/hristo/.rvm/gems/ruby-1.9.2-p290@rails3/gems/json_spec-1.0.3/lib/json_spec/helpers.rb:8:in parse_json': [DEPRECATION] MultiJson.decode is deprecated and will be removed in the next major version. Use MultiJson.load instead. Then the JSON response at "title" should be "Steve Richert meeting" # json_spec-1.0.3/lib/json_spec/cucumber.rb:29 And the JSON response at "start_time" should have in it "2015-09-27" # features/step_definitions/api_steps.rb:74 And the JSON response at "start_time" should have in it "18:30" # features/step_definitions/api_steps.rb:74 And the JSON response at "details/0" should include: # json_spec-1.0.3/lib/json_spec/cucumber.rb:37 """ {"description":null,"duration":60,"id":1,"project_id":1} """ Expected included JSON at path "details/0" (RSpec::Expectations::ExpectationNotMetError) features/meetings/api.feature:43:inAnd the JSON response at "details/0" should include:'

Failing Scenarios: cucumber features/meetings/api.feature:31 # Scenario: Post action

1 scenario (1 failed) 8 steps (1 failed, 7 passed) 0m4.628s

laserlemon commented 12 years ago

It's hard to follow that copy/paste but I think what you're looking for is:

Then the JSON response at "details/0" should be:
  """
  {
    "description": null,
    "duration": 60,
    "id": 1,
    "project_id": 1
  }
  """

Or as a one-liner:

Then the JSON response at "details/0" should be {"description":null,"duration":60,"id":1,"project_id":1}

There is more on all of the matchers available in the README. Hope that helps!

k-solutions commented 12 years ago

Hello,

My fall ... please look at this And the JSON response at "start_time" should have in it "2015-09-27" # features/step_definitions/api_steps.rb:74 And the JSON response at "start_time" should have in it "18:30" # features/step_definitions/api_steps.rb:74

I had to implement my own step definitions to be able to match this. Include just would not match this And the JSON response at "start_time" should include "18:30"

Which seems to me like a valid include match request. And this leads to Include matchers match? method and case inside which would give right result if transformed like this: case actual when Hash then actual.values.map{|v| exclude_keys(v) }.include?(expected) when Array then actual.map{|e| exclude_keys(e) }.include?(expected) when String then actual.include?(expected)

laserlemon commented 12 years ago

Ahh, I think I understand better now. I think I… yup, I like it!

laserlemon commented 12 years ago

Fixed in decc361! :metal: