Problem creating MockController


#1

Hi there,

I’m trying to create a mock controller to test the use of UrlHelper in
ActionMailer. I’m trying this:

require
‘d:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.0/lib/action_controller’

Re-raise errors caught by the controller.

#class DiscussionsController; def rescue_action(e) raise e end; end

class MockController < ActionController::Base
def initialize
@url = UrlRewriter.new(ActionController::TestRequest.new, nil)
super
end
end

because if I just do require ‘action_controller’ I get a no such file
error.

Problem with this mock is that I get a recursive error:

SystemStackError: stack level too deep
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.0/lib/action_controller/helpers.rb:112:in
inherited_without_api' d:/ruby/lib/ruby/gems/1.8/gems/actionwebservice-0.9.3/lib/action_web_service/container/action_controller_container.rb:86:ininherited_without_action_controller’

but I’m not enough of a rails hack to understand why this fails.

Many thanks in advance.

CHEERS> SAM


#2

Well I’m still stumped by this one. I tried moving the MockController
into the rails app/controller directory:

class MockController < ApplicationController
def initialize
@url =
ActionController::UrlRewriter.new(ActionController::TestRequest.new,
nil)
super
end
end

but I still get an error:

MissingSourceFile: No such file to load – url_rewriter
d:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
require__' d:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:inrequire’
d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:214:in
require' d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:39:inrequire_or_load’
d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:22:in
depend_on' d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:178:inrequire_dependency’
d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:178:in
require_dependency' d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:194:inconst_missing’
D:/User/Code/ruby/prometheus/config/…/app/controllers/mock_controller.rb:3:
in `initialize’

I can’t see anything in the UrlRewriter code to suggest that it is
protected or private. Is it a standard thing in ruby that a modules
subclasses can only be accessed from within classes of that module?

CHEERS> SAM


#3

Hmm,

So I have come up with a partial solution by hacking up the url_for
method in ActionController:Base

def url_for(options = {}, *parameters_for_method_reference) #:doc:
case options
when String then options
when Symbol then send(options, *parameters_for_method_reference)
when Hash then begin
@url.rewrite(rewrite_options(options))
rescue
@url = UrlRewriter.new(TestRequest.new, nil)
@url.rewrite(rewrite_options(options))
end
end
end

maybe I’ll submit that as a patch.

CHEERS> SAM


#4

Sam J. wrote:

Hi there,
… snip
because if I just do require ‘action_controller’ I get a no such file
error.

Hi, RailsN00b here, for my own learning, I’m just wondering why not the
following?:

============ code ===========

require ‘rubygems’
require_gem ‘actionpack’

class MockController < ActionController::Base

def initialize
yada.yada.yada
end

end

============ /code ===========

I tried this and got no error (osx10.4/1.8.2/1.0.0), just trying to
understand why this approach is not useable in your particular
situation.

-Amr


#5

Hi Amr,

Thanks for your input. I tried this, but it generated an error:

NameError: uninitialized constant UrlRewriter

d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/depend
encies.rb:200:in `const_missing’

The problem I have is not that I can’t use the ActionController::Base,
but that I can’t seem to use the ActionController::UrlRewriter class, as
indicated by the error above. I have tried all the following
combinations to no avail:

require ‘rubygems’
require_gem ‘actionpack’

class MockController < ActionController::Base
@url = UrlRewriter.new(ActionController::TestRequest.new, nil)
end

require ‘rubygems’
require_gem ‘actionpack’

class MockController < ActionController::Base
@url =
ActionController::UrlRewriter.new(ActionController::TestRequest.new,
nil)
end

require ‘rubygems’
require_gem ‘actionpack’

class MockController < ApplicationController
@url = UrlRewriter.new(ActionController::TestRequest.new, nil)
end

I really don’t understand what it takes to pull in a subclass. However I
seem to be able to work around this by patching the
ActionController::Base class so it can still do rewrites despite @url
being null.

CHEERS> SAM