Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Unit Test namespaced models

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?

like image 386
Chowlett Avatar asked Jul 29 '11 14:07

Chowlett


2 Answers

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) 
like image 121
Chowlett Avatar answered Sep 20 '22 17:09

Chowlett


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
like image 39
apneadiving Avatar answered Sep 20 '22 17:09

apneadiving