-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing Infrastrucutre #1011
Comments
Hi @trek, actually the 0.10.x version wrote from scratch, base on 0.8.0 structure. |
If you happen to know the general direction, point me in it and I'll do the work. It wasn't as simple as just restoring the file :p |
hey @trek, awesome, I think a could start would be something similar to what the 0.9.3 had. Once we have it we can evolve it or add new features, but I think it would be a good starting point. |
Hi, @trek I was the author of For validating schema I have a simple RSpec matcher: require 'json-schema'
# Json schema matcher
# Schema files should be placed at spec/support/fixtures/
#
# @example validate json against bundles.json schema
# expect(json).to match_json_schema('bundles/show')
#
# @example it's also possible to validate test response
# get :show, id: 664
# is_expected.to match_json_schema('bundles/show')
#
RSpec::Matchers.define :match_json_schema do |against|
match do |object_to_be_validated|
file_name = Rails.root.join('json_schemas', "#{against}.json").to_s
fail "Could not find schema file #{file_name}" unless File.exist?(file_name)
@errors = validate(file_name, object_to_be_validated)
return true if @errors.empty?
end
failure_message do
@errors.join("\n")
end
private
def validate(file_name, response_or_json)
json = if response_or_json.is_a?(ActionController::TestResponse)
JSON.parse(response_or_json.body).with_indifferent_access
else
response_or_json
end
JSON::Validator.fully_validate(file_name, json)
end
end I'm sure someone already build gem for this. So my controller specs looks like so: RSpec.describe V1::MoviesController, type: :controller do
let!(:movie) { create(:movie, :with_images) }
context '#index' do
subject do
get :index
end
it { is_expected.to have_http_status :ok }
it { is_expected.to match_json_schema('movies/index') }
end
end I do not test serializer implementation details here, only ensure that response is valid against provided schema. I also unit-test serializers separately, checking all edge-cases and ensure it's valid against |
If you're curious about rendering JSON Schema in docs, look at awesome library docson. It can generates something like this http://lbovet.github.io/docson/index.html#/docson/examples/example.json |
Comparing to JSON Schema is partially how we unit test the serialization itself, but this approach turns your controller unit tests into more of an integration tests. Similar to how tests would behave with I've found this approach to end up being mostly annoying, especially as you're mid-development of the next version of a public API. You change the schema and get, from a unit test perspective, a bunch of false negatives that you need to chase down. Preferably in this situation only the unit tests for serialization and any actual integration tests would report failures. |
Great @bolshakov, I like json-schema, and indeed it seems a good approach. I'm deeply interested into documentation stuff right now 😄 |
@trek look at discussion about deprecating @joaomdmoura if you have some ideas about documenting API, please don't hesitate to share it with us :) |
😁 @bolshakov let's chat on IRC or over email someday. But I'll open an issue to discuss it once I finish deserialization 😄 |
Hi guys, I made a PR to bring back the @bolshakov the gem is json-schema-rspec and here has a post about it too. I really like the idea of @bolshakov I think we can have both helpers the |
Closing in favor of #1099 |
moving to #1099 |
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: #1011 (comment)
I can't tell where (
git log --
returns nothing for that file), but at some point after 0.9.3 the Rails test case integration code was removed.I can't find where the equivalent code is in the current revision, so this breaks controller unit test like
when trying to update, even though that behavior has not changed. Am I just not seeing where this code was moved to?
The text was updated successfully, but these errors were encountered: