On Wednesday, 27 February 2019 12:02:12 UTC+1, Jon Rowe wrote:
>
> If you need a controller you need a controller spec, its not practical to 
> instantiate a controller on your own, one of the many reasons why they are 
> recommended against by the Rails team now.
>
> Otherwise you need to test the behaviour of the end result, e.g. create a 
> set of shared examples for your concern and use them in every 
> request/system/integration test for the routes concerned.
>

I tried it as follows:

#spec/controllers/concerns/response_spec.rb


require 'rails_helper'


class FakeController < ApplicationController
end


RSpec.describe FakeController, type: :controller do
  let(:controller) { FakeController.new}
  
  FakeModel = Struct.new(:name)
  describe 'Response concern' do
    context '#json_response' do
      it 'renders JSON response' do
        fake_model = FakeModel.new('example')
        result = controller.json_response(fake_model)
        puts "result: #{result.inspect}"        
      end
    end
    
  end
end



but it fails with:

rspec spec/controllers/concerns/response_spec.rb 

F


Failures:


  1) FakeController Response concern #json_response renders JSON response

     Failure/Error: render response

     

     Module::DelegationError:

       ActionController::Metal#status= delegated to @_response.status=, but 
@_response is nil: #<FakeController:0x00007fd004810700 @_routes=nil, 
@_request=nil, @_response=nil, @_config={}, @_db_runtime=109.12200000000001>

     # ./app/controllers/concerns/response.rb:6:in `json_response'

     # ./spec/controllers/concerns/response_spec.rb:14:in `block (4 levels) 
in <top (required)>'

     # ------------------

     # --- Caused by: ---

     # NoMethodError:

     #   undefined method `status=' for nil:NilClass

     #   ./app/controllers/concerns/response.rb:6:in `json_response'


Finished in 0.17825 seconds (files took 1.12 seconds to load)

1 example, 1 failure


What am I missing here ?

>
> Jon Rowe
> ---------------------------
> [email protected] <javascript:>
> jonrowe.co.uk
>
> On 27 February 2019 at 11:00, belgoros wrote:
>
> 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 ?
>
>

-- 
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/ceac2db2-3133-4fb7-bb7c-c0e13ff96b8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to