Rails / Rspec: Having an anonymous controller be of a certain class - ruby-on-rails

My controllers inherit actions from ApplicationController. My goal is to test the behaviour of any controller that inherits from ApplicationController. I created RandomController in my specs in order to achieve that goal.
Here is my spec so far
require 'rails_helper'
RSpec.configure do |c|
c.infer_base_class_for_anonymous_controllers = false
end
class RandomController < ApplicationController; end
class Random < ApplicationRecord; end
RSpec.describe RandomController, type: :controller do
controller {}
describe '.index' do
context 'when no record exists' do
before { get :index }
specify { should respond_with(200) }
end
end
end
Here is application_controller
class ApplicationController
def index
binding.pry
end
end
The issue is that when the index method runs, self.class returns #<Class:0x00007f8c33b56fc8> instead of RandomController. Is it possible to have my anonymous controller be an instance of a given controller (declared within the specs) ?

According to the docs you can specify the base class for the anonymous controller:
To specify a different base class you can pass the class explicitly to the
controller method:
controller(BaseController)
https://relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller
Thus you can probably call:
controller(RandomController)
in your specs

Consider using shared_context instead of creating a RandomController to test shared code:
shared_context 'an application controller' do
describe '#index' do
context 'when no record exists' do
before { get :index }
expect(response).to have_http_status(:ok)
end
end
end
You would typically put this file under /spec/support. Example:
/spec/support/shared_contexts_for_application_controllers.rb
Then, in each controller that inherits from ApplicationController:
describe RandomController do
include_context 'an application controller'
end

Related

How to test the callback for any actions in the controller using request.headers?

I have something like this controller:
class ApiApplicationController < ActionController::API
before_action :record_information_from_headers
private
def record_information_from_headers
InformationSet.create(info: request.headers['INFO'])
end
end
All other controllers are inherited from ApiApplicationController
I want to test that my callback works before each method in child controllers. And try to use Anonymous controller
require 'rails_helper'
RSpec.describe ApiApplicationController do
controller(ApiApplicationController) do
def index; end
end
let('INFO') { "some information" }
it 'some' do
get :index
expect(InformationSet.last.info).to eq('some information')
end
end
But, first of all, i have error:
"NoMethodError:
undefined method `controller' for RSpec::ExampleGroups::ApiApplicationController:Class"
And secondly, how do I pass the information to the header ?
I've already read How to test ApplicationController method defined also as a helper method? and Rspec controller test for callback after_save
I would be grateful for any help)
You don't have the type: describe ...Controller, type: :controller do
The Setting request headers section of the docs shows how to set request headers for controller specs.

Rails - How to mock or access methods defined in ApplicationController from helper spec?

I'm trying to write specs for a Rails helper. This helper calls a method
defined in ApplicationController and exposed through helper_method:
app/helpers/monkeys_helper.rb:
module MonkeysHelper
def current_monkey_banana_count
# current_monkey is defined in ApplicationController
current_monkey.present? ? current_monkey.banana_count : 0
end
end
app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
helper_method :current_monkey
protected
def current_monkey
#current_monkey ||= Monkey.find(session[:monkey_id])
end
end
If I call current_monkey_banana_count from a view and access it through the browser, it works fine. But if I call it from a spec like this:
spec/helpers/monkeys_helper_spec.rb:
RSpec.describe MonkeysHelper, type: :helper do
describe "#current_monkey_banana_count" do
it "returns 0 if there is no monkey" do
expect(helper.current_monkey_banana_count).to eq 0
end
end
end
Then I get this error when I run the spec:
NameError:
undefined local variable or method `current_monkey' for #<#<Class:0x007fe1ed38d700>:0x007fe1e9c72d88>
Rspec documentation says:
To access the helper methods you're specifying, simply call them
directly on the helper object. NOTE: helper methods defined in
controllers are not included.
Any idea how to either mock current_monkey or make it visible from inside current_monkey_banana_count?
Thanks!
I found a (nasty) way to do it, but it works:
spec/helpers/monkeys_helper_spec.rb:
require 'rails_helper'
RSpec.describe CartsHelper, type: :helper do
before do
def helper.current_monkey; end
end
describe "#current_monkey_banana_count" do
it "returns 0 if there is no cart" do
expect(helper).to receive(:current_monkey).and_return(nil)
expect(helper.current_monkey_banana_count).to eq 0
end
it "returns monkey.banana_count if there is a monkey" do
expect(helper).to receive(:current_monkey).and_return(Monkey.create!(banana_count: 5))
expect(helper.current_monkey_banana_count).to eq 5
end
end
end
Maybe you can achieve that by mocking current_monkey in this way (have you tried it already?):
RSpec.describe MonkeysHelper, type: :helper do
let(:monkey) { create(:monkey) }
before do
allow(helper).to receive(:current_monkey_user) { monkey }
end
# your rest of code
end
Cheers!
View can call helper methods defined in controller because controller eval them automatically, please check code here.
But your helper test doesn't call controller, so that current_monkey isn't available on MonkeysHelper module. The best practice is helpers defined in controller call helper defined in helper class but not vice versa. In your case, you can move current_monkey to MonkeyHelper to be able to test it.

rspec - Newly instantiated record is nil according to assigns

In my controller:
class CategoriesController < ApplicationController
before_filter :authenticate_super_admin!
def new
#thecategory = Category.new
#thebrands = Brand.all
render "categories/new"
end
end
In my test
require 'rails_helper'
RSpec.describe CategoriesController, type: :controller do
before(:all) do
#the_super_admin = createLoggedInSuperAdmin
end
context "#new" do
it "instantiates new category" do
get :new
expect(assigns(:thecategory)).to be_a_new(Category)
end
end
end
it keeps on telling me expected nil to be a kind of Model and when I inspect it with pry assigns(:themodel) shows as nil
I haven't been able to find any answers that help me with this situation
Turns out I was an idiot, the createLoggedInSuperAdmin call should have been wrapped in before(:each) instead of before(:all), that was why the authentication was working for the first test but failing on the second one

Dynamically add method in before(:all)

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
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

Testing sub application controller

I have a base admin controller that inherits from ApplicationController. In order to test a before filter in Admin::BaseController, I created an anonymous controller in this spec.
require 'spec_helper'
describe Admin::BaseController do
it { should be_a(ApplicationController) }
controller do
def index
render :text => ''
end
end
context 'when current user is not an admin' do
it 'redirects to root path' do
get :index
response.should redirect_to(root_path)
end
end
end
But when I make a request for index action, it doesn't call the before filter in Admin::BaseController.
When I define that filter in ApplicationController instead of Admin::BaseController and run the test, it works. Apparently this anonymous controller inherits from ApplicationController. How can I change this behavior?
I've found the answer here; https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller
controller(Admin::BaseController) do
def index
render :text => ''
end
end
did the trick.

Resources