Autotest and subclasses / namespaces

I am writing a controller admin/cities_controller.rb

it inherits from AdminController, so it’s defined like

class Admin::CitiesController > AdminController

Whenever I save the controller file, autotest freaks out:

uninitialized constant Admin::AdminController (NameError)

I’m pretty used to just hitting CTRL-C to get autotest to re-load all
the files, or flicking to the spec file, and saving it to get a
similar effect.

But it is fairly irritating.

Any suggestions for how to hack autotest to get this to flow more
nicely?

cheers,
Matt

http://blog.mattwynne.net

On Fri, Aug 15, 2008 at 7:42 AM, Matt W. [email protected] wrote:

I am writing a controller admin/cities_controller.rb
it inherits from AdminController, so it’s defined like
class Admin::CitiesController > AdminController
Whenever I save the controller file, autotest freaks out:
uninitialized constant Admin::AdminController (NameError)
I’m pretty used to just hitting CTRL-C to get autotest to re-load all the
files, or flicking to the spec file, and saving it to get a similar effect.
But it is fairly irritating.
Any suggestions for how to hack autotest to get this to flow more nicely?

I’ve seen this happen on occasion but have just done what you’re doing
(workarounds) to get by.

Has anyone experienced this running autotest against test/unit? I
don’t think rspec is doing anything that would interfere with autotest
loading the right files, but it’s possible.

Matt W. wrote:

I’m pretty used to just hitting CTRL-C to get autotest to re-load all
the files, or flicking to the spec file, and saving it to get a
similar effect.

But it is fairly irritating.

Any suggestions for how to hack autotest to get this to flow more nicely?

Hmm… I am using namedspaced controllers on my current project and I am
not running into those issues. I am using rspec edge, ZenTest 3.9.1,
and rails 2.1.0.

Have you copied your code exactly in your original email? If so, I
might see a difference in how we are doing things and that might be your
problem.
My base controller is also namedspaced… So in my app/controllers/admin
dir I have the following:

base_contoller.rb:
module Admin
class BaseController < ::ApplicationController

end
end

Then:

other_controller.rb:
module Admin
class OtherController < BaseController

end
end

Note that I could also do:

base_contoller.rb:
class Admin::BaseController < ApplicationController

other_controller.rb:
class Admin::OtherController < Admin::BaseController

So… If your AdminController is namespaced you should change your other
controllers to:
class Admin::CitiesController < Admin::AdminController

If it is not namespaced (but, it really should be IMO) then you could
try this:
class Admin::CitiesController < ::AdminController

Hope that helps,
Ben

Thanks for the tips Ben.

We upgraded to Rails 2.1 (from 1.x) on Friday and this seems to have
gone away. I’ll report back though if I learn anything else.

cheers,
Matt

http://blog.mattwynne.net

On Mon, Aug 18, 2008 at 8:20 AM, Matt W. [email protected] wrote:

Nope, despite my efforts to repro this earlier, it’s started happening
again.

Check out the output from autotest:

/usr/local/bin/ruby -S script/spec -O spec/spec.opts
/Users/matt/Documents/projects/songkick/skweb/app/controllers/admin/cities_controller.rb

The fact that autotest is trying to load the controller file itself is
probably the source of the problem here.

I’m sure you noted this earlier, but what version of rspec and zentest
are you using? Have you modified the mappings in rails_rspec.rb (you
can do this in RAILS_ROOT/.autotest or ~/.autotest)?

Nope, despite my efforts to repro this earlier, it’s started
happening again.

Check out the output from autotest:

/usr/local/bin/ruby -S script/spec -O spec/spec.opts /Users/matt/
Documents/projects/songkick/skweb/app/controllers/admin/
cities_controller.rb spec/controllers/admin/cities_controller_spec.rb
/Users/matt/Documents/projects/songkick/skweb/app/controllers/admin/
cities_controller.rb:1: uninitialized constant Admin (NameError)
from /Users/matt/Documents/projects/songkick/skweb/vendor/
plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in load' from /Users/matt/Documents/projects/songkick/skweb/vendor/ plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in load_files’
from /Users/matt/Documents/projects/songkick/skweb/vendor/
plugins/rspec/lib/spec/runner/example_group_runner.rb:13:in each' from /Users/matt/Documents/projects/songkick/skweb/vendor/ plugins/rspec/lib/spec/runner/example_group_runner.rb:13:in load_files’
from /Users/matt/Documents/projects/songkick/skweb/vendor/
plugins/rspec/lib/spec/runner/options.rb:98:in run_examples' from /Users/matt/Documents/projects/songkick/skweb/vendor/ plugins/rspec/lib/spec/runner/command_line.rb:19:in run’
from script/spec:4
/usr/local/bin/ruby -S script/spec -O spec/spec.opts /Users/matt/
Documents/projects/songkick/skweb/app/controllers/admin/
cities_controller.rb spec/controllers/admin/cities_controller_spec.rb

Finished in 0.183342 seconds

16 examples, 0 failures

The first time, I saved the controller, and it bombed. The second
time, I switched to the spec and saved that. Each time it looks as
though the same parameters are being passed to script/spec, but
something different is happening after that…

I’ve tried variously referring to the base class ::AdminController,
as Ben suggested but it doesn’t seem to make any difference.

Right now, it looks like

class Admin::CitiesController < ::AdminController

Perhaps the reason the spec works is that it shares some behaviour
with the AdminController specs:

require File.expand_path(File.dirname(FILE) +
‘/…/…/spec_helper’)
require File.expand_path(File.dirname(FILE) + ‘/…/
admin_controller_spec’)

describe Admin::CitiesController do

it_should_behave_like "all admin controllers"

… etc

I’m using ZenTest 3.10.0 / Rails 2.1 / RSpec 1.1.4

cheers,
Matt

http://blog.mattwynne.net

In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.

On 18 Aug 2008, at 14:27, David C. wrote:

Check out the output from autotest:

/usr/local/bin/ruby -S script/spec -O spec/spec.opts
/Users/matt/Documents/projects/songkick/skweb/app/controllers/
admin/cities_controller.rb

The fact that autotest is trying to load the controller file itself is
probably the source of the problem here.

I had changed the controller file though (I’m refactoring). Would you
expect autotest to still just load the corresponding spec, rather
than the changed controller source file itself?

I’m sure you noted this earlier, but what version of rspec and zentest
are you using?

I’m using ZenTest 3.10.0 / Rails 2.1 / RSpec 1.1.4

Have you modified the mappings in rails_rspec.rb (you
can do this in RAILS_ROOT/.autotest or ~/.autotest)?

I do have an ~/.autotest but it’s only got Growl stuff in it, and
there’s nothing in my rails root.

cheers,
Matt

On Mon, Aug 18, 2008 at 8:39 AM, Matt W. [email protected] wrote:

I had changed the controller file though (I’m refactoring). Would you expect
autotest to still just load the corresponding spec, rather than the changed
controller source file itself?

That’s correct. Autotest should only load the spec files and they,
in turn, should require anything they need. I’m not saying this is an
autotest bug. It’s getting it’s direction as to what files to load
when from the mappings set up in rails_rspec.rb and .autotest, so the
bug is likely in there somewhere.

I do have an ~/.autotest but it’s only got Growl stuff in it, and there’s
nothing in my rails root.

K - that shouldn’t be the issue.

Why don’t you try getting the latest rspec from git and see if the
problem persists.