views:

216

answers:

2

Hi there,

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 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's name

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

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

btelles
super(*args) is fine when the subclass's initialize doesn't care about the arguments except to pass them along to the superclass initialize. Did you know you can just say "super" and Ruby will automatically pass on all of the arguments? To call the superclass method with no arguments, you'd have to say "super()".
Wayne Conrad
Cool, I didn't know that. Thanks!
btelles
+1  A: 

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.

John Hyland
Thanks John. Have a good one!
btelles