In my Rails app, I make heavy use of subdirectories of the model directory and hence of namespaced models - that is, I have the following directory heirarchy:
models
|
+- base_game
| |
| +- foo.rb (defines class BaseGame::Foo)
|
+- expansion
|
+- bar.rb (defines class Expansion::Bar)
This is enforced by Rails - that is, it Just Doesn't Work to call the classes "just" Foo
and Bar
.
I want to unit test these classes (using Shoulda on top of ActiveSupport::TestCase). I know that the name of the class-under-test will be derived from the name of the test case class.
How do I write unit tests that will test these classes?
Turns out it is straight-forward, even under normal ActiveSupport
/ Test::Unit
. Simply duplicate the model directory structure under /test
:
test
|
+- base_game
| |
| +- foo_test.rb (defines class BaseGame::FooTest)
|
+- expansion
|
+- bar_test.rb (defines class Expansion::BarTest)
This should work directly, I use Rspec and it's straight:
describe User::Profile do
it "should exist" do
User::Profile.class.should be_a Class
end
it { should validate_presence_of(:name) }
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With