Open ysyyork opened 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?
@bf4 So the correct way to test should be using json schema?
@ysyyork I do think that would be better.
@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.
@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?
@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.
@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.
@beauby this sounds reasonable.
@bf4 Sorry, is there any link saying rails is removing controller testing?
@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)
@bf4 Thanks a lot. I searched but didn't find. So I decide to ask to get free food ;)
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)