Skip to content
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

Bring back assert_serializer #1099

Closed
wants to merge 6 commits into from

Conversation

maurogeorge
Copy link
Member

Hi guys,

closes #1011

The assert_serializer test helper was removed at some point.

This patch brings back the assert_serializer test helper. This is the last
revision1 that has the helper. The original helper was used as base.

@bf4
Copy link
Member

bf4 commented Aug 30, 2015

nice

@trek
Copy link
Contributor

trek commented Sep 2, 2015

@maurogeorge looks like this needs a rebase.

@maurogeorge
Copy link
Member Author

@trek rebased. Thanks for poiting

@@ -335,6 +335,34 @@ class PostSerializer < ActiveModel::Serializer
end
```

## Test helpers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maurogeorge could you move this to an article on our new docs and maybe but a reference here in the README?
Maybe into the "How to" section explaining hot to assert serializer.

@joaomdmoura
Copy link
Member

@maurogeorge this looks nice, thank you for your contribution 😄 we made some comments on it, can you check it?

@maurogeorge maurogeorge force-pushed the patch-02 branch 2 times, most recently from bef35ff to 6455272 Compare September 9, 2015 12:36
@maurogeorge
Copy link
Member Author

Hi @joaomdmoura thanks for the review. Updated with the changes 😄

@maurogeorge
Copy link
Member Author

I think that the broken test on the AppVeyor is not related with this PR, this test is broken on the master too.

ActiveSupport.on_load(:action_controller) do
include ::ActionController::Serialization
ActionDispatch::Reloader.to_prepare do
ActiveModel::Serializer.serializers_cache.clear
end
ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather put something like this in active_model_serializers/test.rb, no? like

require 'active_support/test_case'

module ActiveJob
  class TestCase < ActiveSupport::TestCase
    include ActiveJob::TestHelper
  end
end
module ActiveJob
  extend ActiveSupport::Autoload

  autoload :TestCase
  autoload :TestHelper
end

then

class ActiveJobTestCaseTest < ActiveJob::TestCase

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review ❤️

I try something in this line, but got no sucess 😞 since Active Job defines the ActiveJob::TestCase in our case we do not want to define our TestCase class but mixes the helper in the ActionController::TestCase.

I followed the same idea we have in the in the ActionController::TestCase. I renamed/move the assertions module to a better place.

Let me know what you think

@maurogeorge
Copy link
Member Author

@joaomdmoura @bf4 sorry for the delay I was involved with other things 😞

I update the code following the @bf4 last comments. I waiting for your review guys ❤️

@trek
Copy link
Contributor

trek commented Oct 14, 2015

Appears to be more merge conflicts :-/

@maurogeorge
Copy link
Member Author

@trek I solved the conflicts, thanks for the report.

@@ -44,6 +44,7 @@ def silence_warnings
require 'active_model/serializer'
require 'active_model/serializable_resource'
require 'active_model/serializer/version'
require 'active_model/serializer/assertions'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be eager-loaded here as it's a test-only concern

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.

I think if we try to autoload ActiveModel::Serializer::Assertions, this will be loaded when we mix this on the ActionController::TestCase, in this same file, so this wil be loaded too soon.
Do you have some idea how we can lazy load this?

@bf4
Copy link
Member

bf4 commented Oct 14, 2015

fwiw, here's some RSpec stuff I wrote for rc1 #878 (comment) that I've been meaning to work into ams in #967

@maurogeorge
Copy link
Member Author

@bf4 thanks for the review 💯 , I answered each directly.

Pretty cool the RSpec stuff.

In my opinion I think we can try to generate test helpers like this and this this way we do not depend on RSpec, and the people that are using RSpec can mix this module and use with RSpec too, like we do for example with the ActiveSupport::Testing::TimeHelpers.

What you think?

@bf4
Copy link
Member

bf4 commented Oct 15, 2015

Reading comments via email now (no code) but sounds good

B mobile phone

On Oct 15, 2015, at 4:50 PM, Mauro George [email protected] wrote:

@bf4 thanks for the review , I answered each directly.

Pretty cool the RSpec stuff.

In my opinion I think we can try to generate test helpers like this and this this way we do not depend on RSpec, and the people that are using RSpec can mix this module and use with RSpec too, like we do for example with the ActiveSupport::Testing::TimeHelpers.

What you think?


Reply to this email directly or view it on GitHub.

@maurogeorge maurogeorge force-pushed the patch-02 branch 2 times, most recently from c99bbb7 to dba5be4 Compare November 27, 2015 10:15
@maurogeorge
Copy link
Member Author

@bf4 @joaomdmoura @beauby I update the code could you guys please review? Thanks

@maurogeorge maurogeorge force-pushed the patch-02 branch 3 times, most recently from 9e19b19 to 1329cc8 Compare December 3, 2015 20:41
@maurogeorge
Copy link
Member Author

@bf4 @joaomdmoura @beauby I updated to keep all tests green. Thanks

@trek
Copy link
Contributor

trek commented Dec 20, 2015

@bf4 I don't suppose this could get merged or closed? Poor @maurogeorge has to keep rebasing to keep it current with master.

@beauby
Copy link
Contributor

beauby commented Dec 20, 2015

This looks good to me. @bf4 @joaomdmoura?

@assert_serializer.expectation = expectation
@assert_serializer.message = message
@assert_serializer.response = response
assert(@assert_serializer.matches?, @assert_serializer.message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, people using RSpec would need to define assert(expected, actual) and any library not using ActionController::TestCase would also need to include ActiveModelSerializers::Test::Serializer (I forget if RSpec-Rails uses this)

something like:

module AMSAssertSerializer
  extend ActiveSupport::Concern
  included do
    include ActiveModelSerializers::Test::Serializer
  end
  def assert(expected, actual)
    expect(actual).to eq(expected)
  end
end
RSpec.configuration.include AMSAssertSerializer, type: :controller

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review.

I don't think we need to care with RSpec and other libs at the moment, Action Mailer uses Minitest methods and Active Job too.

I think we can follow the same rationale, we are providing Minitest test helpers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know. :)

@bf4
Copy link
Member

bf4 commented Dec 21, 2015

Looking good. I had a few questions above and I'm ok to merge.

@trek @maurogeorge is this something you'd like to use? Just confirming because I kind of agree with @bolshakov that it doesn't seem very useful. But, given that I'm thinking/saying this 4 months after the PR was opened, if people want to use it, I'll merge it.

@@ -51,6 +51,7 @@ def silence_warnings
require 'active_model/serializer'
require 'active_model/serializable_resource'
require 'active_model/serializer/version'
require 'active_model_serializers/test/serializer'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this should only be loaded in the test environment. Sell me on always loading it, and why it won't cause issues in production (including the railtie ActionController::TestCase.send(:include, ActiveModelSerializers::Test::Serializer)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and #1270 (comment)

The `assert_serializer` test helper was removed at some point.

This patch brings back the `assert_serializer` test helper. This is the last
revision[1] that has the helper. The original helper was used as base.

[1]: https://github.com/rails-api/active_model_serializers/tree/610aeb2e9297fa31b8d561f0be9a4597f0258f8c
Rails 5 does not include `assert_template` but we need this on the tests of
the helper.

This add the `rails-controller-testing` to keep support on `assert_template`.
@maurogeorge
Copy link
Member Author

@trek @bf4 @beauby I updated the code, based on the last comments.

The Travis is broken, but I don't think this is related with this patch, It seems that these tests already be broken on master.

@bf4
Copy link
Member

bf4 commented Dec 21, 2015

@maurogeorge I have a PR to fix master, but wanted some eyes on it #1384

You're right, not related

@trek
Copy link
Contributor

trek commented Dec 22, 2015

Yes, this is something we'd use in unit tests for controllers. We have separate unit tests for the serializers themselves (so ensure they match the a described JSON Schema), but are also currently reusing this strategy for controller tests, which seems a bit odd.

@bf4
Copy link
Member

bf4 commented Dec 22, 2015

@trek so you're saying you used but don't think it's a good idea? (ref what's I've done in the past)

@maurogeorge
Copy link
Member Author

Closing this PR, since the code was moved to the new one #1390

@trek
Copy link
Contributor

trek commented Dec 23, 2015

@bf4 no, I'm saying schema validation in both unit tests for a serializer and as part of a unit test of a controller means you get twice the failures.

@bf4
Copy link
Member

bf4 commented Dec 23, 2015

@trek so you basically are saying if someone is using assert_json_schema, there's no need for assert_serializer? Makes sense. Do you think both option should exist i.e. be supported by AMS and its people?

@trek
Copy link
Contributor

trek commented Dec 28, 2015

@bf4 I think which one you use will depend on your testing strategy. We've moved on from wanting assert_serializer just due to the need to get coding. But, now we get multiple test failures if someone changes the schema accidentally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Testing Infrastrucutre
5 participants