Forum: RSpec Error in executing controller spec

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5ef1fb684daf370e1601381ae01e75e7?d=identicon&s=25 Amit Kulkarni (ak123)
on 2009-05-08 13:03
Hello,

     I am trying to execute controller spec but it gives me error which
says

     @controller is nil: make sure you set it in your test's setup
method.

I tried to search for the solution but till now no success :(

Following is the description of the controller spec.

describe ChannelCategoriesController do

  context "Admin" do

    before(:each) do
      @cha_category = ChannelCategory.new
    end

      it "should delete categories" do
        delete :destroy, :id => 1
      end
 end
end

Please advice :-)
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-08 18:29
(Received via mailing list)
On Fri, May 8, 2009 at 4:03 AM, Amit Kulkarni <lists@ruby-forum.com>
wrote:
> Following is the description of the controller spec.
>        delete :destroy, :id => 1
>      end
>  end
> end
>
> Please advice :-)

Questions:

* ruby version
* rspec version
* rails version
* OS
* what's the path to the file
* how are you running the spec (rake, spec command, etc)
* pls provide a full backtrace
  * best in a gist or pastie
  * script/spec path/to/file --backtrace

Cheers,
David
C6ce9a98479a04c47d143d444ae317c6?d=identicon&s=25 Kero van Gelder (Guest)
on 2009-05-10 21:45
(Received via mailing list)
> Questions:
>
[...]
> * pls provide a full backtrace
>   * best in a gist or pastie

Imagine someone looking at the problem report
two years from now. No pastie. Then the
solution (if provided) comes without a complete problem.

Like code without a test.

Put it in an attachment, or whatever, but keep the two together,
please.

Bye,
Kero.

___
How can I change the world if I can't even change myself?
  -- Faithless, Salva Mea
5ef1fb684daf370e1601381ae01e75e7?d=identicon&s=25 Amit Kulkarni (ak123)
on 2009-05-19 10:30
Ruby version : 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
rspec - 1.2.4
rails - 2.1.2
OS - Vista Home
I am running a single file by the command
spec channels_controller(filename)_spec.rb

Contents of the file:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

def setup
  @controller  = ChannelsController.new
  @request     = ActionController::TestRequest.new
  @response    = ActionController::TestResponse.new
end

def user_attributes
    {
      :login => 'foo',
      :password => 'foobar',
      :password_confirmation => 'foobar',
      :email => 'foo@dibistore.it',
      :privacy => '1',
      :idiom => nil,
      :role_id => 2
    }
end

describe ChannelsController do
    fixtures :channels, :users
    context "Brand Manager" do
    @user = User.find_by_login("amit")
      it "should not create channel" do
        user = User.new
        user.attributes = user_attributes
        post :create, :channel => {:brand_name => 'by manager'}
      end
    end
end

Now when i run this file i get error which says
RuntimeError in 'ChannelsController Brand Manager should not create
chnnel'
@controller is nil: mke sure you set it in your test's setup method.

Backtrace is as follows:
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/ctionpck-2.1.2/lib/ction_controller/
test_process.rb:378:in `process'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/ctionpck-2.1.2/lib/ction_controller/
test_process.rb:376:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/ctionpck-2.1.2/lib/ction_controller/
test_process.rb:376:in `process'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/ctionpck-2.1.2/lib/ction_controller/
test_process.rb:365:in `post'
./chnnels_controller_spec.rb:141:
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/exmple/exmple_m
ethods.rb:41:in `instnce_evl'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/exmple/exmple_m
ethods.rb:41:in `execute'
C:/Progrm Files/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/exmple/exmple_m
ethods.rb:38:in `execute'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/exmple/exmple_g
roup_methods.rb:203:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/exmple/exmple_g
roup_methods.rb:201:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/exmple/exmple_g
roup_methods.rb:201:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/exmple/exmple_g
roup_methods.rb:99:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/runner/exmple_g
roup_runner.rb:23:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/runner/exmple_g
roup_runner.rb:22:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/runner/exmple_g
roup_runner.rb:22:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/runner/options.
rb:119:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/lib/spec/runner/commnd_l
ine.rb:9:in `run'
C:/Progrm Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.4/bin/spec:4
C:/Progrm Files/ruby/bin/spec:19:in `lod'
C:/Progrm Files/ruby/bin/spec:19

Please Advice
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-19 15:36
(Received via mailing list)
On Tue, May 19, 2009 at 3:30 AM, Amit Kulkarni <lists@ruby-forum.com>
wrote:
> Ruby version : 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
> rspec - 1.2.4
> rails - 2.1.2
> OS - Vista Home
> I am running a single file by the command
> spec channels_controller(filename)_spec.rb

Where is this file? For rspec-rails to know that it's supposed to be a
controller spec, it either needs to be inside the
PROJECT_ROOT/spec/controllers directory, or you need to change the
describe declaration to this:

describe ChannelsController, :type => :controller do

>
> Contents of the file:
>
> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>
> def setup
>  @controller  = ChannelsController.new
>  @request     = ActionController::TestRequest.new
>  @response    = ActionController::TestResponse.new
> end

Get rid of this setup. First of all, RSpec does this for you
automatically inside controller example groups. Secondly, this is
outside any example group (describe block), so it's not related to
anything anyway.

>    }
> end

I'd move user_attributes inside the example group too. That way it's
scoped. If not, it's going to be available to (and possibly in
conflict with) any files that get loaded after this one.

> end
>
> Now when i run this file i get error which says
> RuntimeError in 'ChannelsController Brand Manager should not create
> chnnel'
> @controller is nil: mke sure you set it in your test's setup method.

Uh, where are all the 'a's in the backtrace?
5ef1fb684daf370e1601381ae01e75e7?d=identicon&s=25 Amit Kulkarni (ak123)
on 2009-05-19 16:04
Hi,

Thanks for quick response

My channels_controller_spec.rb file is inside controller spec only
i.e. PROJECT_ROOT/spec/controllers/channels_controller_spec.rb.

As per your suggestion i removed the setup method and moved
user_attributes under example groups.

but i am getting the same error which says  RuntimeError in
'ChannelsController Brand Manager should not create channel'
@controller is nil: make sure you set it in your test's setup method.

Still confused why is this coming again.

Following is the backtrace(Regarding a's its a minor prob.will do it)
RuntimeError in 'ChannelsController Brand Manager should not create
channel'
@controller is nil: make sure you set it in your test's setup method.
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_control
ler/test_process.rb:378:in `process'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_control
ler/test_process.rb:376:in `each'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_control
ler/test_process.rb:376:in `process'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_control
ler/test_process.rb:365:in `post'
./channels_controller_spec1.rb:149:
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/example/exampl
e_methods.rb:40:in `instance_eval'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/example/exampl
e_methods.rb:40:in `execute'
C:/Program Files/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/example/exampl
e_methods.rb:37:in `execute'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/example/exampl
e_group_methods.rb:207:in `run_examples'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/example/exampl
e_group_methods.rb:205:in `each'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/example/exampl
e_group_methods.rb:205:in `run_examples'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/example/exampl
e_group_methods.rb:103:in `run'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/example
_group_runner.rb:23:in `run'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/example
_group_runner.rb:22:in `each'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/example
_group_runner.rb:22:in `run'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/options
.rb:119:in `run_examples'
C:/Program
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/command
_line.rb:9:in `run'
C:/Program Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/bin/spec:4
C:/Program Files/ruby/bin/spec:19:in `load'
C:/Program Files/ruby/bin/spec:19

> Where is this file? For rspec-rails to know that it's supposed to be a
> controller spec, it either needs to be inside the
> PROJECT_ROOT/spec/controllers directory, or you need to change the
> describe declaration to this:
>
> describe ChannelsController, :type => :controller do
>
>>
>> Contents of the file:
>>
>> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>>
>> def setup
>> �@controller �= ChannelsController.new
>> �@request � � = ActionController::TestRequest.new
>> �@response � �= ActionController::TestResponse.new
>> end
>
> Get rid of this setup. First of all, RSpec does this for you
> automatically inside controller example groups. Secondly, this is
> outside any example group (describe block), so it's not related to
> anything anyway.
>
>> � �}
>> end
>
> I'd move user_attributes inside the example group too. That way it's
> scoped. If not, it's going to be available to (and possibly in
> conflict with) any files that get loaded after this one.
>
>> end
>>
>> Now when i run this file i get error which says
>> RuntimeError in 'ChannelsController Brand Manager should not create
>> chnnel'
>> @controller is nil: mke sure you set it in your test's setup method.
>
> Uh, where are all the 'a's in the backtrace?
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-19 16:51
(Received via mailing list)
On Tue, May 19, 2009 at 9:04 AM, Amit Kulkarni <lists@ruby-forum.com>
wrote:
> but i am getting the same error which says  RuntimeError in
> Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_control
> ./channels_controller_spec1.rb:149:
It looks like you're calling the file from the spec/controllers
directory. Just for fun, step up to the project root and run:

script/spec spec/controllers/channels_controller_spec1.rb
5ef1fb684daf370e1601381ae01e75e7?d=identicon&s=25 Amit Kulkarni (ak123)
on 2009-05-20 08:39
David Chelimsky wrote:
> On Tue, May 19, 2009 at 9:04 AM, Amit Kulkarni <lists@ruby-forum.com>
> wrote:
>> but i am getting the same error which says  RuntimeError in
>> Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_control
>> ./channels_controller_spec1.rb:149:
> It looks like you're calling the file from the spec/controllers
> directory. Just for fun, step up to the project root and run:
>
> script/spec spec/controllers/channels_controller_spec1.rb

Hey It worked thanks a lot. :-)

Can you tell me the difference why it was not running earlier and now it
is running.Just for my understanding.
5ef1fb684daf370e1601381ae01e75e7?d=identicon&s=25 Amit Kulkarni (ak123)
on 2009-05-20 08:47
Amit Kulkarni wrote:
> David Chelimsky wrote:
>> On Tue, May 19, 2009 at 9:04 AM, Amit Kulkarni <lists@ruby-forum.com>
>> wrote:
>>> but i am getting the same error which says  RuntimeError in
>>> Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_control
>>> ./channels_controller_spec1.rb:149:
>> It looks like you're calling the file from the spec/controllers
>> directory. Just for fun, step up to the project root and run:
>>
>> script/spec spec/controllers/channels_controller_spec1.rb
>
> Hey It worked thanks a lot. :-)
>
> Can you tell me the difference why it was not running earlier and now it
> is running.Just for my understanding.

It is not the case that i will always run spec from the root project.So
if i want to run from spec/controllers direcrtory then what should i do
since i am getting the error.
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-20 09:37
(Received via mailing list)
On Wed, May 20, 2009 at 1:47 AM, Amit Kulkarni <lists@ruby-forum.com>
wrote:
>>> script/spec spec/controllers/channels_controller_spec1.rb
>>
>> Hey It worked thanks a lot. :-)
>>
>> Can you tell me the difference why it was not running earlier and now it
>> is running.Just for my understanding.
>
> It is not the case that i will always run spec from the root project.So
> if i want to run from spec/controllers direcrtory then what should i do
> since i am getting the error.

Unfortunately, you're going to have to run it from the project root
because that's the only it works, apparently. You can file a bug
report at http://rspec.lighthouseapp.com if you're interested.

Cheers,
David
5ef1fb684daf370e1601381ae01e75e7?d=identicon&s=25 Amit Kulkarni (ak123)
on 2009-05-20 09:47
David Chelimsky wrote:
> On Wed, May 20, 2009 at 1:47 AM, Amit Kulkarni <lists@ruby-forum.com>
> wrote:
>>>> script/spec spec/controllers/channels_controller_spec1.rb
>>>
>>> Hey It worked thanks a lot. :-)
>>>
>>> Can you tell me the difference why it was not running earlier and now it
>>> is running.Just for my understanding.
>>
>> It is not the case that i will always run spec from the root project.So
>> if i want to run from spec/controllers direcrtory then what should i do
>> since i am getting the error.
>
> Unfortunately, you're going to have to run it from the project root
> because that's the only it works, apparently. You can file a bug
> report at http://rspec.lighthouseapp.com if you're interested.
>
> Cheers,
> David

Thank a lot.
But is the case that all controller specs are run from the root
directory.
I had had written spec models and it runs fine from inside the spec
directory.

I will sure file a ticket on the site u mentioned.
Also it will be good if i get to know the reason behind this :-)
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-20 16:17
(Received via mailing list)
On Wed, May 20, 2009 at 2:47 AM, Amit Kulkarni <lists@ruby-forum.com>
wrote:
>>> It is not the case that i will always run spec from the root project.So
> Thank a lot.
> But is the case that all controller specs are run from the root
> directory.
> I had had written spec models and it runs fine from inside the spec
> directory.
>
> I will sure file a ticket on the site u mentioned.
> Also it will be good if i get to know the reason behind this :-)

It's not by design. It's a bug.
5ef1fb684daf370e1601381ae01e75e7?d=identicon&s=25 Amit Kulkarni (ak123)
on 2009-05-21 12:11
David Chelimsky wrote:
> On Wed, May 20, 2009 at 2:47 AM, Amit Kulkarni <lists@ruby-forum.com>
> wrote:
>>>> It is not the case that i will always run spec from the root project.So
>> Thank a lot.
>> But is the case that all controller specs are run from the root
>> directory.
>> I had had written spec models and it runs fine from inside the spec
>> directory.
>>
>> I will sure file a ticket on the site u mentioned.
>> Also it will be good if i get to know the reason behind this :-)
>
> It's not by design. It's a bug

Ok.
Thanks a lot.
This topic is locked and can not be replied to.