# Can I add a singleton action to @controller in a test?

**URL:** <https://www.ruby-forum.com/t/can-i-add-a-singleton-action-to-controller-in-a-test/136973>\
**Category:** Rails\
**Created:** [May 7, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/can-i-add-a-singleton-action-to-controller-in-a-test/136973 "2008-05-07T21:32:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [May 7, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/can-i-add-a-singleton-action-to-controller-in-a-test/136973/1 "2008-05-07T21:32:47Z")

</div>

All,

I want to create a mock action to test a filter in a functional test,  
e.g.:

# Simplified example

def test\_my\_filter  
# Singleton action  
def @controller.foo  
assert # something  
end

```
ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/foo', :action => 'foo'
end

get :foo

```

end

My log files show that no action responds to foo, so I think I’m not  
doing the routing properly. I’m trying this in  
ApplicationControllerTest, if that matters. Is anything obviously wrong  
here?

Thanks for taking the time to read this,

Brian

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [May 7, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/can-i-add-a-singleton-action-to-controller-in-a-test/136973/2 "2008-05-07T23:12:38Z")

</div>

On 7 May 2008, at 20:32, Brian H. wrote:

> My log files show that no action responds to foo, so I think I’m not  
> doing the routing properly. I’m trying this in  
> ApplicationControllerTest, if that matters. Is anything obviously  
> wrong  
> here?

The routing stuff is irrelevant as routing isn’t used in functional  
tests (the tests know that since you’ve got @controller you want to  
invoke actions from there). If you look at the action controller code,  
rails decides whether or not a action exists by looking at the class’  
instance methods, so singleton methods don’t get a look in. could you  
not just define a method in the class (and undef it afterwards if you  
want) or ever create a controller class on the fly just with that  
action it

## Fred

My ramblings: [http://www.spacevatican.org](http://www.spacevatican.org)

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [May 8, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/can-i-add-a-singleton-action-to-controller-in-a-test/136973/3 "2008-05-08T19:13:03Z")

</div>

Frederick C. wrote:  
[…]

> The routing stuff is irrelevant as routing isn’t used in functional  
> tests (the tests know that since you’ve got @controller you want to  
> invoke actions from there). If you look at the action controller code,  
> rails decides whether or not a action exists by looking at the class’  
> instance methods, so singleton methods don’t get a look in. could you  
> not just define a method in the class (and undef it afterwards if you  
> want) or ever create a controller class on the fly just with that  
> action it
> 
> ## Fred
> 
> My ramblings: [http://www.spacevatican.org](http://www.spacevatican.org)

I did the latter and it worked very well. Thanks Fred!
