0

Im testing a Module that can be included in a Controller. Right now the code looks like that:

class GithubAuthorizationController < ApplicationController
  include Frontend::Concerns::GithubAuthorization

  def index
    render inline: "lorem_ipsum"
  end
end

describe GithubAuthorizationController do
  before(:all) do
    @page_content = "lorem_ipsum"
  end
  ...........

As you can see I basically create a Test-Controller before the tests are run. Now I would like to add the module and index method in the before(:all)-block. I tried:

class GithubAuthorizationController < ApplicationController
end

describe GithubAuthorizationController do
  before(:all) do
    @page_content = "lorem_ipsum"

    class < @controller
      include Frontend::Concerns::GithubAuthorization

      def index
        render inline: "lorem_ipsum"
      end
    end
  end
  ...........

As I can see in debugger in the before(:all) block the @controller is defined as <GithubAuthorizationController .... So It is a instance. There Is also no error when running the code, but the tests fails, because of The action 'index' could not be found ...

What do I wrong? How can I move the code to the before(:all) block? Thanks

1 Answer 1

2

The way to do this in rspec is with a controller block:

describe GithubAuthorizationController, type: :controller do
  context "with module" do
    controller do
      include Frontend::Concerns::GithubAuthorization

      def index
        render inline: "lorem_ipsum"
      end
    end

    # within this block the controller will be the anonymous controller class created above 
  end
end

If you have set infer_base_class_for_anonymous_controllers to false (this is not the default) then you need to do controller(GithubAuthorizationController) or you'll inherit directly from ApplicationController

Your issue could be down to a missing route - the controller helper creates some routes for you (for the default index, show etc.) actions. You can add extra ones in an example with

 it "does something with a custom route" do 
  routes.draw { get "custom" => "github_authorization#custom" } 
  get :custom
    ...
end
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, great answer. I would really like to use this solution. But right now Im getting ` method_missing': undefined method controller' for Class.Do you have an idea why it fails? Can you send me the docs for controller do. I was not able to find them. Thanks
Ok seems to work with control_class. Great. Now Im getting the error with missing routes. Where would you add the routes.draw { get "custom" => "github_authorization#custom" } block? in the context?
You may need type: :controller on the spec, as I have just added. See relishapp.com/rspec/rspec-rails/v/3-4/docs/controller-specs/…
Thanks! Great Answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.