# Dynamic determination of class under test

**URL:** <https://www.ruby-forum.com/t/dynamic-determination-of-class-under-test/63249>\
**Category:** Rails\
**Created:** [June 13, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/dynamic-determination-of-class-under-test/63249 "2006-06-13T16:17:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [June 13, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/dynamic-determination-of-class-under-test/63249/1 "2006-06-13T16:17:10Z")

</div>

I’ve been developing a series of custom assertions for testing my models  
and it  
looks like I can unify most some of the assertions if I can find a good  
way to  
determine the class under test at run-time. Now I refactor my test code  
rather  
vigorously so its not as simple as just stripping “Test” off the name of  
the  
test case class, but I have a few thoughts on a useful convention I can  
use to  
ease it.

Now this is probably more of a (near novice) Ruby question rather than  
rails,  
but I can’t seem to locate the answer – given a string containing a  
class name  
is there a way to  
a) check if that class is defined/locatable for inclusion  
b) convert the string to a Class object for the named class

Ie, I’m trying to do:

def setup  
@klass=class\_under\_test(self.class)  
end

protected  
def class\_under\_test(klass)  
/([A-Z][a-z\_]\*)Test/.match(klass.to\_s)  
$1 # \<----- Now I have a string, not a Class, obviously  
# Of course I need a more complete alogrithm here, but I’d like  
to  
# to get the base case working first, do I need to use one of the  
# eval-type functions?  
end

def assert\_attribute\_required(field)  
@non\_inserted\_fixture[field]=nil  
obj = @klass.new(@non\_inserted\_fixture) # \<---- Now I need a Class  
object  
assert\_not\_valid obj  
assert obj.errors.invalid?(field)  
end

Thank you.

Eric

---

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [June 13, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/dynamic-determination-of-class-under-test/63249/2 "2006-06-13T16:46:49Z")

</div>

I am not sure if I get your question right… maybe this is what you  
need:

require ‘set’

def acts\_as\_test  
TestClasses.get \<\< self  
end

class TestClasses  
@@test\_classes = Set.new  
def self.get  
@@test\_classes  
end  
end

class TestMe  
acts\_as\_test  
end

class TestMeToo  
acts\_as\_test  
end

> TestClasses.get.inspect  
> #\<Set: {TestMeToo, TestMe}\>

-------- Original-Nachricht --------  
Datum: Tue, 13 Jun 2006 10:14:54 -0400  
Von: Eric D Nielsen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
An: removed\_email\_address@domain.invalid  
Betreff: [Rails] Dynamic determination of class under test…

---

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [June 13, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/dynamic-determination-of-class-under-test/63249/3 "2006-06-13T16:53:53Z")

</div>

> def assert\_attribute\_required(field)  
> @non\_inserted\_fixture[field]=nil  
> obj = @klass.new(@non\_inserted\_fixture) # \<---- Now I need a Class  
> object  
> assert\_not\_valid obj  
> assert obj.errors.invalid?(field)  
> end

For this part, thanks to ActiveSupport, you would have something like:  
…  
obj = @klass.constantize.new()  
…

That will get you your new class object to work with.

Thank you.

---

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [June 13, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/dynamic-determination-of-class-under-test/63249/4 "2006-06-13T18:06:50Z")

</div>

Nick S. wrote:

> > def assert\_attribute\_required(field)  
> > @non\_inserted\_fixture[field]=nil  
> > obj = @klass.new(@non\_inserted\_fixture) # \<---- Now I need a Class  
> > object  
> > assert\_not\_valid obj  
> > assert obj.errors.invalid?(field)  
> > end
> 
> For this part, thanks to ActiveSupport, you would have something like:  
> …  
> obj = @klass.constantize.new()  
> …

Thank you, that was one half of what I was looking for and I’m  
positively I can figure out the rest now that the base case works.

Eric
