开发者

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

开发者 https://www.devze.com 2022-12-19 07:37 出处:网络
I\'ve been using Rspec for a while and for some reason am receiving errors on a controller called ReferencesController.

I've been using Rspec for a while and for some reason am receiving errors on a controller called ReferencesController.

The error says that I have to specify the controller name by either using:

 describe MyController do

or

 describe 'aoeuaoeu' do
   controller_name :my

I've tried both variations:

 describe ReferencesController do

and

 describe 'refs controller' do
   controller_name :references

But I get an error for both! Any Idea what could be going on wrong?

Berns

EDIT: Due to the nature of the solution, I've reworded the title and added relevant code. Here's the erroneous 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 开发者_如何学Pythonthe 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's name

        describe "a MyController" do
            controller_name :my #invokes the MyController
    end


If you call super(args), you're passing in a single argument - the array referenced by args. Using the "splat operator" - super(*args) - turns the array in to a list and passes along each element of args as an individual argument.

As Wayne has pointed out, there's also a little syntactic sugar in Ruby that lets you just say super and it will automatically pass on the arguments for you, treating it like super(*args) instead of just super().

In your particular case, I would guess that your Controller's superclass's initialize method doesn't accept an array, so when RSpec tried to instantiate your Controller it failed, which ultimately resulted in the error message you saw.


Doh! Figured it out...

The initialize method had "super(args)" instead of "super(*args)"

If anyone wants to rewrite this answer and give a full explanation, (or perhaps explain why I should not define an instance variable in that manner) I'll be happy to up-vote and give you the accepted answer.

Bernie

0

精彩评论

暂无评论...
验证码 换一张
取 消