# Problem creating MockController

**URL:** <https://www.ruby-forum.com/t/problem-creating-mockcontroller/94878>\
**Category:** Rails\
**Created:** [January 10, 2006, 1:08am UTC](https://www.ruby-forum.com/t/problem-creating-mockcontroller/94878 "2006-01-10T01:08:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@samj](https://www.ruby-forum.com/u/samj)\
**Post date:** [January 10, 2006, 1:08am UTC](https://www.ruby-forum.com/t/problem-creating-mockcontroller/94878/1 "2006-01-10T01:08:27Z")

</div>

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:in`inherited\_without\_action\_controller’

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

Many thanks in advance.

CHEERS\> SAM

---

<div class="post-metadata">

**Author:** ![samj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@samj](https://www.ruby-forum.com/u/samj)\
**Post date:** [January 10, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/problem-creating-mockcontroller/94878/2 "2006-01-10T18:54:12Z")

</div>

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:in`require’  
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:in`require\_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:in`require\_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:in`const\_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

---

<div class="post-metadata">

**Author:** ![samj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@samj](https://www.ruby-forum.com/u/samj)\
**Post date:** [January 10, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/problem-creating-mockcontroller/94878/3 "2006-01-10T19:00:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![samj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@samj](https://www.ruby-forum.com/u/samj)\
**Post date:** [January 10, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/problem-creating-mockcontroller/94878/4 "2006-01-10T22:34:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![samj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@samj](https://www.ruby-forum.com/u/samj)\
**Post date:** [January 13, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/problem-creating-mockcontroller/94878/5 "2006-01-13T19:36:56Z")

</div>

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
