# Regarding Testing Mixins

**URL:** <https://www.ruby-forum.com/t/regarding-testing-mixins/86972>\
**Category:** Ruby\
**Created:** [March 25, 2006, 5:44am UTC](https://www.ruby-forum.com/t/regarding-testing-mixins/86972 "2006-03-25T05:44:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 25, 2006, 5:44am UTC](https://www.ruby-forum.com/t/regarding-testing-mixins/86972/1 "2006-03-25T05:44:06Z")

</div>

How does one write a unit test for a mixin? I gathered since our unit  
test is a class (TestClassName \< Test::Unit::TestCase), we could just  
include said module into said class. However, I’ve ran into a problem.

Suppose my project directory is as follows:

…/project\_dir/trunk/lib # Where all my classes and scripts go  
…/project\_dir/trunk/tests # Where all my tests go

My mixin resides in the lib folder, and normally I use this neat trick  
to handle this discreprancy in file locations:

$:.unshift File.join(File.dirname( **FILE** ), “…”, “lib”) # Thank you  
Pick Ax!

Why doesn’t this seem to work for mixins? Where does Ruby look for  
mixins?

I’m assuming it’s a path problem despite the fact that Ruby groans that  
the name I’m providing it is an unitialized constant (e.g. include  
Mixin – if I write include “Mixin”, Ruby tells me “wrong argument type  
String (expected Module)”)

Thank you,

James H.

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 25, 2006, 6:09am UTC](https://www.ruby-forum.com/t/regarding-testing-mixins/86972/2 "2006-03-25T06:09:51Z")

</div>

On Sat, 25 Mar 2006, James H. wrote:

> handle this discreprancy in file locations:
> 
> Thank you,
> 
> James H.

a) you still must require your mixin using the **FILE** method  
b) then you mix it in as normal

eg

require ‘mixin’

class Test \< Test::Unit::TestCase  
include Mixin  
end

make sense?

regards.

-a

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 25, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/regarding-testing-mixins/86972/3 "2006-03-25T17:30:18Z")

</div>

On Mar 24, 2006, at 10:43 PM, James H. wrote:

> Suppose my project directory is as follows:
> 
> …/project\_dir/trunk/lib # Where all my classes and scripts go  
> …/project\_dir/trunk/tests # Where all my tests go
> 
> My mixin resides in the lib folder, and normally I use this neat  
> trick to handle this discreprancy in file locations:
> 
> $:.unshift File.join(File.dirname( **FILE** ), “…”, “lib”) # Thank  
> you Pick Ax!

Just FYI, you can also solve this by running your tests (from the  
root project directory) with something like:

$ ruby -I lib:test test/ts\_all.rb

If you use Rake, it will automatically add the directories for your  
test tasks.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 25, 2006, 6:14am UTC](https://www.ruby-forum.com/t/regarding-testing-mixins/86972/4 "2006-03-25T06:14:07Z")

</div>

On 2006-03-25 00:09:47 -0500, removed\_email\_address@domain.invalid said:

> > String (expected Module)")
> 
> -a  
> Indeed! Thank you. I feel like a dolt, but now I’ll never forget again  
> =)

James H.
