# Testing a singleton method

**URL:** <https://www.ruby-forum.com/t/testing-a-singleton-method/134140>\
**Category:** Ruby\
**Created:** [April 8, 2008, 8:18am UTC](https://www.ruby-forum.com/t/testing-a-singleton-method/134140 "2008-04-08T08:18:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [April 8, 2008, 8:18am UTC](https://www.ruby-forum.com/t/testing-a-singleton-method/134140/1 "2008-04-08T08:18:54Z")

</div>

Trying to pass a unit test;

def test\_singleton\_method  
string1 = “hello”  
string2 = “world”

```
assert string1.respond_to?(:only_one)
assert !string2.respond_to?(:only_one)

```

end

====

my code to pass it:

string1 = “hello”  
string2 = “world”

def string1.only\_one  
puts “PLEASE WORK”  
end

===

yet, this doesn’t work, is there something I’m missing?

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [April 8, 2008, 8:33am UTC](https://www.ruby-forum.com/t/testing-a-singleton-method/134140/2 "2008-04-08T08:33:37Z")

</div>

I’m not exactly sure what you are trying to test, but this works:

def test\_singleton\_method  
string1 = “hello”  
string2 = “world”  
def string1.only\_one  
puts “PLEASE WORK”  
end  
assert string1.respond\_to?(:only\_one)  
assert !string2.respond\_to?(:only\_one)  
end

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [April 8, 2008, 8:48am UTC](https://www.ruby-forum.com/t/testing-a-singleton-method/134140/3 "2008-04-08T08:48:16Z")

</div>

Could you post more context?

Chances are the singleton you _think_ you’re testing is not the same  
instance you actually _are_ testing.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
