rails-api / active_model_serializers

ActiveModel::Serializer implementation and Rails hooks
MIT License
5.33k stars 1.39k forks source link

How to unit test each_serializer #1970

Open ysyyork opened 8 years ago

ysyyork commented 8 years ago

Expected behavior vs actual behavior

I use assert_serializer helper to unit test but it cannot work on each_serailizer. Expect to have a way test each_serializer.

Steps to reproduce

(e.g., detailed walkthrough, runnable script, example application)

Environment

ActiveModelSerializers Version (commit ref if not on tag): 0.10.2

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]

OS Type & Version: MacOS 10.12.1

Integrated application and version (e.g., Rails, Grape, etc): rails 4.2.5

Backtrace

(e.g., provide any applicable backtraces from your application)

Additonal helpful information

(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)

bf4 commented 8 years ago

@ysyyork I don't think assert_serializer is a particularly useful thing to use or test, as it doesn't really protect against any regression, right?

ysyyork commented 8 years ago

@bf4 So the correct way to test should be using json schema?

bf4 commented 8 years ago

@ysyyork I do think that would be better.

beauby commented 8 years ago

@ysyyork You should unit-test your controllers to make sure it does what you want it to do, and expose the right data. Then your serialization lib (here AMS) should be tested so that it does what it's supposed to do with the data you provide it). Finally, you should have some integration tests that ensure your whole app works as expected, and that is making a few scenarios of request/responses that ensure specific payloads are sent and received.

ysyyork commented 8 years ago

@beauby Thanks for your reply. One more question. When you are saying testing controllers, how can I make sure they are using the correct serializers? Before this post, I thought I should use assert_serializer to check it and then use json schema in integration test to further ensure I get the correct data. But As @bf4 said, it seems using assert_serializer is not a proper way to test. Then I am just wondering what other approaches I can use to test serializers in controller test? Or should I just ignore it in controller test?

bf4 commented 8 years ago

@beauby Rails is removing 'controller' tests in favor of 'integration/request' tests. I'd lean toward calling that an integration test rather than unit test.

@ysyyork I don't think assert_serializer is a very useful test. You can make an assertion about the response body and that will cover testing the serializer.

beauby commented 8 years ago

@bf4 By "unit-testing controllers" I meant unit-testing whatever bits of code it calls.

@ysyyork Your customers probably do not care what serializer is used and neither should you. What you should test is the behavior, not the implementation, and you do that by doing "black box" integration tests.

ysyyork commented 8 years ago

@beauby this sounds reasonable.

ysyyork commented 8 years ago

@bf4 Sorry, is there any link saying rails is removing controller testing?

bf4 commented 8 years ago

@ysyyork I'll go google that for you.. http://blog.bigbinary.com/2016/04/19/changes-to-test-controllers-in-rails-5.html :) (That post also references assert_template which is what assert_serializer is based on)

ysyyork commented 8 years ago

@bf4 Thanks a lot. I searched but didn't find. So I decide to ask to get free food ;)