I want to do unit testing on my controllers in a Rails 5 API only environment. I want to make sure that the correct serializer is used for serializing the response. I therefore want to use the assert_serializer assertion.
Expected: Serializer is tested, actual: Rails 5 doesn't know the assert_serializer function. Error log on rake test:
NameError: undefined local variable or method `assert_response_schema' for #<UsersControllerTest:0x007fa4f80a24f0>
Did you mean? assert_response
Make a new Rails 5 API only project, include active_model_serializers and try to run the assertion in a test
ActiveModelSerializers Version _(commit ref if not on tag)_:
0.10.2
Output of ruby -e "puts RUBY_DESCRIPTION":
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
OS Type & Version:
OSX El Capitan
Integrated application and version _(e.g., Rails, Grape, etc)_:
Rails 5
@BScharbau Thanks for writing. Are you using minitest or ActionController::TestCase or rspec-rails? This is a critical question since https://github.com/rails-api/active_model_serializers/blob/v0.10.2/lib/active_model_serializers/railtie.rb#L43-L46 is how the test helpers are mixed in. In other news, that's how you can mix them in yourself.
I'm using minitest with ActionDispatch::IntegrationTest, which has become standard in Rails 5. ActionController::TestCase has become deprecated in Rails 5.
I'm also running into the same issue, NoMethodError: undefined method 'assert_serializer' in my controller tests ActionDispatch::IntegrationTest.
@nguyenj @BScharbau
Based on my comment above https://github.com/rails-api/active_model_serializers/issues/1939#issuecomment-250187451 it appears you can just
if Rails.env.test?
ActionController::IntegrationTest.include ActiveModelSerializers::Test::Schema
ActionController::IntegrationTest.include ActiveModelSerializers::Test::Serializer
end
In test setup.
Though honestly, I don't think it's a very useful test. assert_template, which this is based on, was also removed. If it's important enough to assert what serializer was called, you should probably just specify it. That also means you save lookup time.
That makes sense. Thanks for your quick response!
How would I set this up for Rspec?
Edit: found a way to do this.
# rails_helper.rb
RSpec.configure do |config|
...
config.include ActiveModelSerializers::Test::Schema
config.include ActiveModelSerializers::Test::Serializer
end
can we wrap it up, as https://github.com/rails-api/active_model_serializers/issues/1939#issuecomment-299091351 and https://github.com/rails-api/active_model_serializers/issues/1939#issuecomment-502283184 address different ways to achieve the target?
or do we still need to address something to resolve the case. thanks