Error in executing controller spec

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 :frowning:

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 :slight_smile:

On Fri, May 8, 2009 at 4:03 AM, Amit K. [email protected]
wrote:

Following is the description of the controller spec.
delete :destroy, :id => 1
end
end
end

Please advice :slight_smile:

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

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

On Tue, May 19, 2009 at 3:30 AM, Amit K. [email protected]
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?

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:ineach’
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:inpost’
./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:inexecute’
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:inexecute’
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:ineach’
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:inrun’
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:ineach’
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:inrun_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:inload’
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?

On Tue, May 19, 2009 at 9:04 AM, Amit K. [email protected]
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

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 => ‘[email protected]’,
: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:inech’
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:inpost’
./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:inexecute’
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:inexecute’
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:inech’
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:inrun’
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:inech’
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:inrun_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:inlod’
C:/Progrm Files/ruby/bin/spec:19

Please Advice

David C. wrote:

On Tue, May 19, 2009 at 9:04 AM, Amit K. [email protected]
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. :slight_smile:

Can you tell me the difference why it was not running earlier and now it
is running.Just for my understanding.

Amit K. wrote:

David C. wrote:

On Tue, May 19, 2009 at 9:04 AM, Amit K. [email protected]
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. :slight_smile:

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.

On Wed, May 20, 2009 at 1:47 AM, Amit K. [email protected]
wrote:

script/spec spec/controllers/channels_controller_spec1.rb

Hey It worked thanks a lot. :slight_smile:

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

David C. wrote:

On Wed, May 20, 2009 at 1:47 AM, Amit K. [email protected]
wrote:

script/spec spec/controllers/channels_controller_spec1.rb

Hey It worked thanks a lot. :slight_smile:

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 :slight_smile:

On Wed, May 20, 2009 at 2:47 AM, Amit K. [email protected]
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 :slight_smile:

It’s not by design. It’s a bug.

David C. wrote:

On Wed, May 20, 2009 at 2:47 AM, Amit K. [email protected]
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 :slight_smile:

It’s not by design. It’s a bug

Ok.
Thanks a lot.