How Unit Test Models with Names

In my Rails application, I actively use subdirectories of the model directory and therefore models with names, that is, I have the following directory hierarchy:

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 "Paste" on top of ActiveSupport :: TestCase). I know that the class name under the test will be derived from the class name of the test case.

How to write unit tests that will test these classes?

+4
source share
3 answers

It turns out that this is straightforward, even with normal ActiveSupport / Test::Unit . Just duplicate the model directory structure in /test :

 test | +- base_game | | | +- foo_test.rb (defines class BaseGame::FooTest) | +- expansion | +- bar_test.rb (defines class Expansion::BarTest) 
+4
source

This should work directly, I use Rspec and this directly:

 describe User::Profile do it "should exist" do User::Profile.class.should be_a Class end it { should validate_presence_of(:name) } end 
+2
source

Here is the code that worked for me with both ruby -I and zeus :

 require "test_helper" class BaseGame::Foo < ActiveSupport::TestCase include BaseGame should "let #baz do something" do assert Foo.new.baz end end 

You can also do

 require "test_helper" class BaseGame::Foo < ActiveSupport::TestCase should "let #baz do something" do assert BaseGame::Foo.new.baz end end 
0
source

All Articles