-
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
Bring back assert_serializer #1099
Conversation
nice |
@maurogeorge looks like this needs a rebase. |
b9cfccc
to
8ae9222
Compare
@trek rebased. Thanks for poiting |
@@ -335,6 +335,34 @@ class PostSerializer < ActiveModel::Serializer | |||
end | |||
``` | |||
|
|||
## Test helpers |
There was a problem hiding this comment.
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.
@maurogeorge this looks nice, thank you for your contribution 😄 we made some comments on it, can you check it? |
bef35ff
to
6455272
Compare
Hi @joaomdmoura thanks for the review. Updated with the changes 😄 |
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
6455272
to
fde7f5d
Compare
@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 ❤️ |
Appears to be more merge conflicts :-/ |
@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' |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
fwiw, here's some RSpec stuff I wrote for rc1 #878 (comment) that I've been meaning to work into ams in #967 |
@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 What you think? |
Reading comments via email now (no code) but sounds good B mobile phone
|
c99bbb7
to
dba5be4
Compare
@bf4 @joaomdmoura @beauby I update the code could you guys please review? Thanks |
9e19b19
to
1329cc8
Compare
@bf4 @joaomdmoura @beauby I updated to keep all tests green. Thanks |
@bf4 I don't suppose this could get merged or closed? Poor @maurogeorge has to keep rebasing to keep it current with master. |
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I know. :)
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' |
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
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`.
a7360f4
to
d40b30e
Compare
@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. |
@maurogeorge I have a PR to fix master, but wanted some eyes on it #1384 You're right, not related |
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. |
@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) |
Closing this PR, since the code was moved to the new one #1390 |
@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. |
@trek so you basically are saying if someone is using |
@bf4 I think which one you use will depend on your testing strategy. We've moved on from wanting |
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 lastrevision1 that has the helper. The original helper was used as base.