collectiveidea / json_spec

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

include_json does not working #9

Closed a-chernykh closed 13 years ago

a-chernykh commented 13 years ago

Hi,

Thanks for awesome gem.

I am testing with RSpec with json_spec 0.7.0.

    it 'should create new temp image' do
      post :create, :temp_image => {:file => fixture_file_upload(File.join(Rails.root, 'spec', 'fixtures', 'test_image.jpg'), 'image/jpeg')}, :format => 'json'
      puts response.body
      response.body.should include_json(%({"status":"success"}))
    end

Output is

{"temp_image":{"current_filter":"original","url":"http://picyou.dev/storage/test/filtered/7/original_original_preview.jpg?1314790553","id":7},"status":"success"}

But test fails for some reason with message: Expected included JSON.

Any help would be appreciated.

Thanks, Andrey.

laserlemon commented 13 years ago

The error message could probably be more descriptive (ideas?) but this is the expected behavior. The body of your response is a JSON object that is a superset of the object you're testing against but it doesn't technically include that JSON. For example:

%({"one":1,"two":2}).should include_json(%({"one":1}))

will fail, as you've shown above. However:

%([{"one":1},{"two":2}]).should include_json(%({"one":1}))

or even:

%({"one":1,"two":2}).should include_json(%(1))

will pass. But in the case of your test, I think you could go a simpler route:

response.body.should be_json_eql(%("success")).at_path("status")

or:

response.body.should be_json_eql(%({"status":"success"})).excluding("temp_image")

Hope that helps!

a-chernykh commented 13 years ago

Thank you very much for explanation, that solves my problem.

Andrey.