Rspec: Is there a problem with rspec-rails when initializing a controller class using super (args)?

I have been using Rspec for some time and for some reason I get errors on a controller called ReferencesController.

The error says that I should specify the name of the controller using:

describe MyController do 

or

  describe 'aoeuaoeu' do controller_name :my 

I tried both options:

  describe ReferencesController do 

and

  describe 'refs controller' do controller_name :references 

But I get an error for both! Any idea what might happen wrong?

Burns

EDIT . Due to the nature of the solution, I reworked the header and added the appropriate code. Here's the error code:

# references_controller.rb

 class ReferencesController < ApplicationController def initialize(*args) #do stuff super(args) # <= this is the problem line end def index end end 

And the error:

  1) 'ReferencesController GET index should take parameters for a company and return the references' FAILED Controller specs need to know what controller is being specified. You can indicate this by passing the controller to describe(): describe MyController do or by declaring the controller name describe "a MyController" do controller_name :my #invokes the MyController end 
+4
source share
2 answers

If you call super(args) , you pass one argument - the array referenced by args . Using the "splat operator" - super(*args) - turns the array into a list and passes args for each element as a separate argument.

As Wayne pointed out, there is also some syntactic sugar in Ruby that just lets say super , and it will automatically pass arguments to you, treating it as super(*args) , not just super() .

In your specific case, I would suggest that your Controller initialize superclass method does not accept an array, so when RSpec tried to instantiate your controller, it failed, which ultimately led to the error message you saw.

+3
source

Doh! Figured it out ...

In the initialization method, instead of "super (* args)"

"super (args)" was applied

If someone wants to rewrite this answer and give a full explanation (or perhaps explain why I should not define the instance variable in this way), I will be happy to vote and give you the accepted answer.

Burnie

+1
source

Source: https://habr.com/ru/post/1299978/


All Articles