{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1947","repository_url":"https://api.github.com/repos/rails-api/active_model_serializers","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1947/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1947/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1947/events","html_url":"https://github.com/rails-api/active_model_serializers/pull/1947","id":183508492,"number":1947,"title":"Adds :use_serializer and :have_valid_schema RSpec matchers","user":{"login":"leonelgalan","id":727774,"avatar_url":"https://avatars.githubusercontent.com/u/727774?v=3","gravatar_id":"","url":"https://api.github.com/users/leonelgalan","html_url":"https://github.com/leonelgalan","followers_url":"https://api.github.com/users/leonelgalan/followers","following_url":"https://api.github.com/users/leonelgalan/following{/other_user}","gists_url":"https://api.github.com/users/leonelgalan/gists{/gist_id}","starred_url":"https://api.github.com/users/leonelgalan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leonelgalan/subscriptions","organizations_url":"https://api.github.com/users/leonelgalan/orgs","repos_url":"https://api.github.com/users/leonelgalan/repos","events_url":"https://api.github.com/users/leonelgalan/events{/privacy}","received_events_url":"https://api.github.com/users/leonelgalan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-10-17T19:42:56Z","updated_at":"2016-10-18T01:58:32Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1947","html_url":"https://github.com/rails-api/active_model_serializers/pull/1947","diff_url":"https://github.com/rails-api/active_model_serializers/pull/1947.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/1947.patch"},"body":"#### Purpose\r\nProvides built-in matchers for RSpec users. Example:\r\n\r\n```ruby\r\nrequire 'rails_helper'\r\n\r\nActiveModelSerializers.config.schema_path = 'spec/support/schemas'\r\n\r\ndescribe V1::PostsController do\r\n  include ActiveModelSerializers::RSpec::Matchers::Serializer\r\n  include ActiveModelSerializers::RSpec::Matchers::Schema\r\n\r\n  describe '#index' do\r\n    before { get :index }\r\n\r\n    describe 'response' do\r\n      subject { response }\r\n      it { is_expected.to use_serializer V1::PostSerializer }\r\n      it { is_expected.to have_valid_schema }\r\n    end\r\n  end\r\nend\r\n```\r\n\r\n#### Changes\r\n\r\nAdds two modules:\r\n- `ActiveModelSerializers::RSpec::Matchers::Serializer`\r\n- `ActiveModelSerializers::RSpec::Matchers::Schema`\r\n\r\n#### Caveats\r\n- No tests, I'm still trying to figure out how to run rspec next to minitest and translate _test/active_model_serializers/test/schema_test.rb_ and _test/active_model_serializers/test/serializer_test.rb_ to RSpec.\r\n- Couldn't use `autoload`, expected path would be _/active_model_serializers/r_spec/matchers_, as opposed to: _/active_model_serializers/rspec/matchers_\r\n- Had to `require 'minitest/autorun'` on _lib/active_model_serializers/test/schema.rb_, otherwise I would get the following error:\r\n```\r\nGem Load Error is: uninitialized constant ActiveModelSerializers::Test::Schema::Minitest\r\nBacktrace for gem load error is:\r\n/Users/leonelgalan/Work/active_model_serializers/lib/active_model_serializers/test/schema.rb:33:in `<module:Schema>'\r\n/Users/leonelgalan/Work/active_model_serializers/lib/active_model_serializers/test/schema.rb:3:in `<module:Test>'\r\n/Users/leonelgalan/Work/active_model_serializers/lib/active_model_serializers/test/schema.rb:2:in `<module:ActiveModelSerializers>'\r\n/Users/leonelgalan/Work/active_model_serializers/lib/active_model_serializers/test/schema.rb:1:in `<top (required)>'\r\n/Users/leonelgalan/Work/active_model_serializers/lib/active_model_serializers/rspec/matchers/schema.rb:1:in `<top (required)>'\r\n```\r\n\r\nI could use some help solving this issues.\r\n\r\n#### Related GitHub issues\r\n- [#1470 - Rspec tests & assert_response_schema](https://github.com/rails-api/active_model_serializers/issues/1470)\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/254385540","html_url":"https://github.com/rails-api/active_model_serializers/pull/1947#issuecomment-254385540","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1947","id":254385540,"user":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"created_at":"2016-10-18T01:58:32Z","updated_at":"2016-10-18T01:58:32Z","body":"I read you. the problem with `require` with how you have it is that when `lib/active_model_serializers/rspec/matchers.rb` is required, the matcher files are also required. So for those that don't use rspec, or even during production use, that would lead to these rspec helpers erroring, because not all of the methods are defined. for the initial require of `matchers`, maybe you'll want do something like:\r\n\r\n```ruby\r\nautoload RSpecMatchers, 'rspec/rspec_matchers' if defined? RSpec\r\n```\r\n\r\nI believe that will also protect against accidental requiring of the matchers in a non-test environment, provided that RSpec is only in the test env. But, def something you want to test out.\r\n\r\nAlso note that I changed the name of RSpec/Matchers to RSpecMatchers -- this is to prevent any module/class changes to rspec in the future. \r\n\r\nSo,\r\n```ruby\r\n# lib/active_model_serializers/rspec/matchers.rb\r\nmodule ActiveModelSerializers\r\n  module RSpecMatchers\r\n    # @api public\r\n    # ...\r\n```\r\n\r\nI *think* that'll work, but anywho, give it a go?!\r\n\r\nThanks for your work on this! I'll def use these matchers in my own project :-) "}}
{"actor":{"display_login":"NullVoxPopuli"}}