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.

Cheers
Jon Rowe
---------------------------
[email protected]
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/dejalu-217-fc5202fd-f309-4135-86c3-78a029060c7b%40jonrowe.co.uk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to