On Wednesday, 27 February 2019 11:49:28 UTC+1, Jon Rowe wrote: > > Hi > > A concern is just a bunch of methods you include into a class. You can > test them either independently by bringing the concern into a plain old > class, or if you need to use controller methods you can bring them into a > controller. There is a anonymous controller in controller specs you can use > for this purpose. > > However the Rails team have deprecated controller tests (and therefore > controller specs) in favour of request specs, because the way that Rails > controllers operate are not well suited towards unit tests (they are always > a form of integration test due to the way that the Rails stack was > designed). > > So it depends on what you are testing. > > Thank you, Jon, for your response. As I'd like to test just the methods defined in the above module (concern), as far as I understood, I could put a test no matter in which folder under *spec* directory. For example:
#spec/controllers/concerns/response_spec.rb require 'rails_helper' class FakeController < ApplicationController end RSpec.describe Response do end So if it is OK, the above FakeController should include my concern methods. But how to test them ? Cheers > Jon Rowe > --------------------------- > [email protected] <javascript:> > jonrowe.co.uk > > On 27 February 2019 at 10:46, belgoros wrote: > > I can't figure out the right way to test controller concern methods. What > kind of spec should it be, - controller or other? > Here is a concern I'd like to test: > > #controllers/concerns/response.rb > module Response > > extend > ActiveSupport::Concern > > > > > def json_response(object, status = :ok, opts = {}) > > response > = {json: object, status: status}.merge(opts) > > render response > > end > > > > > def respond_with_errors(object) > > render json > : { errors: ErrorSerializer.serialize(object) }, status: : > unprocessable_entity > > end > > > > > def paginated_response_status(collection) > > collection > .size > WillPaginate.per_page ? :partial_content : : > ok > > end > end > > > The ApplicationController includes the above concern as follows: > > #controllers/application_controller.rb > > class ApplicationController < ActionController:: > API > include > Response > ... > end > > > Thank you! > > -- You received this message because you are subscribed to the Google Groups "rspec" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/76dbc017-1d89-4513-95e8-7bb824d77260%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
