Regarding Testing Mixins

J

James Herdman

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 Herdman
 
A

ara.t.howard

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 Herdman

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
 
J

James Herdman

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

Indeed! Thank you. I feel like a dolt, but now I'll never forget again =)

James Herdman
 
J

James Edward Gray II

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 Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top