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.
Amit K. (Guest)
on 2009-05-08 15: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 :-)
David C. (Guest)
on 2009-05-08 20:29
(Received via mailing list)
On Fri, May 8, 2009 at 4:03 AM, Amit K. <removed_email_address@domain.invalid>
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
Kero van Gelder (Guest)
on 2009-05-10 23: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
Amit K. (Guest)
on 2009-05-19 12: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 => 'removed_email_address@domain.invalid',
      :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
David C. (Guest)
on 2009-05-19 17:36
(Received via mailing list)
On Tue, May 19, 2009 at 3:30 AM, Amit K. <removed_email_address@domain.invalid>
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?
Amit K. (Guest)
on 2009-05-19 18: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?
David C. (Guest)
on 2009-05-19 18:51
(Received via mailing list)
On Tue, May 19, 2009 at 9:04 AM, Amit K. <removed_email_address@domain.invalid>
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
Amit K. (Guest)
on 2009-05-20 10:39
David C. wrote:
> On Tue, May 19, 2009 at 9:04 AM, Amit K. <removed_email_address@domain.invalid>
> 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.
Amit K. (Guest)
on 2009-05-20 10:47
Amit K. wrote:
> David C. wrote:
>> On Tue, May 19, 2009 at 9:04 AM, Amit K. <removed_email_address@domain.invalid>
>> 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.
David C. (Guest)
on 2009-05-20 11:37
(Received via mailing list)
On Wed, May 20, 2009 at 1:47 AM, Amit K. <removed_email_address@domain.invalid>
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
Amit K. (Guest)
on 2009-05-20 11:47
David C. wrote:
> On Wed, May 20, 2009 at 1:47 AM, Amit K. <removed_email_address@domain.invalid>
> 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 :-)
David C. (Guest)
on 2009-05-20 18:17
(Received via mailing list)
On Wed, May 20, 2009 at 2:47 AM, Amit K. <removed_email_address@domain.invalid>
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.
Amit K. (Guest)
on 2009-05-21 14:11
David C. wrote:
> On Wed, May 20, 2009 at 2:47 AM, Amit K. <removed_email_address@domain.invalid>
> 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.