# Unit tests and application.rb

**URL:** <https://www.ruby-forum.com/t/unit-tests-and-application-rb/133353>\
**Category:** Rails\
**Created:** [March 30, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/unit-tests-and-application-rb/133353 "2008-03-30T20:21:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ma6rl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@ma6rl](https://www.ruby-forum.com/u/ma6rl)\
**Post date:** [March 30, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/unit-tests-and-application-rb/133353/1 "2008-03-30T20:21:50Z")

</div>

Am fairly new to Rails so this may be an obvious question but I would  
appreciate any help I can get.

I am working on an existing application and trying to add some unit  
tests (there aren’t any tests to date).

Firstly When I run a unit test I notice that it calls app/controller/  
application.rb which is not something I was expecting. This is  
something I see happening even in a freshly generated rails project  
running the default generated unit tests. I am guessing this is  
correct behaviour but am curious as to why running a unit test on a  
model class results in application.rb being called.

Other than wanting to get a better understanding of Rails the other  
reason I am asking the question is that it is causing the tests to  
fail. This is because in application.rb someone has added the  
following code:

class ApplicationController \< ActionController::Base

layout “application”  
include BasicAuth

$currentsite = Site.find(1)  
$sitefooter=$currentsite.footer  
$sitename=$currentsite.name  
$buttons=Button.find(:all, :order =\> “position”)

…  
end

There is no Site with an ID of 1 as it has been created. I have  
attempted to create a Sites fixture to solve this problem and  
referencing it in the unit test. This did not work so I tried adding  
it to the test\_helper.rb class again this did not work. Looking at the  
stack trace the reason seems to be the order of execution.

In my unit test class I have the following

require File.dirname( **FILE** ) + ‘/…/test\_helper’

class PollTest \< ActiveSupport::TestCase

fixture :sites

# Replace this with your real tests.

def test\_truth  
assert true  
end  
end

this calls test\_helper.rb which in has the following lines

ENV[“RAILS\_ENV”] = “test”  
require File.expand\_path(File.dirname( **FILE** ) + “/…/config/  
environment”)  
require ‘test\_help’

class Test::Unit::TestCase

…  
end

It is the third line which seems to result in application.rb being  
called. This happens before my fixtures are loaded and the test blows  
up.

I have included the static trace below for addition information:

/Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
base.rb:1267:in `find_one': Couldn't find Site with ID=1 (ActiveRecord::RecordNotFound) from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ base.rb:1250:in`find\_from\_ids’  
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
base.rb:504:in `find' from /Users/rich/Documents/Dev/firework/app/controllers/ application.rb:14 from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/1.8/rubygems/custom_require.rb:27:in`gem\_original\_require’  
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/1.8/rubygems/custom\_require.rb:27:in `require' from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/ active_support/dependencies.rb:496:in`require’  
from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/  
active\_support/dependencies.rb:342:in `new_constants_in' from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/ active_support/dependencies.rb:496:in`require’  
… 8 levels…  
from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/  
active\_support/dependencies.rb:496:in `require' from ./test/unit/../test_helper.rb:3 from test/unit/poll_test.rb:1:in`require’  
from test/unit/poll\_test.rb:1

How can I load the fixtures first to ensure there is a Site by the  
time application.rb gets called. The other option is to re-factor  
application.rb. Is the code I have above bad practise?

Many thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![ma6rl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@ma6rl](https://www.ruby-forum.com/u/ma6rl)\
**Post date:** [March 31, 2008, 2:09am UTC](https://www.ruby-forum.com/t/unit-tests-and-application-rb/133353/2 "2008-03-31T02:09:34Z")

</div>

ma6rl wrote:

> I am working on an existing application and trying to add some unit  
> tests (there aren’t any tests to date).

Sometimes a rewrite is in order. Start a new project, write tests-first,  
and  
each time a test fails for the correct reason, copy in a line of the old  
program.

> Firstly When I run a unit test I notice that it calls app/controller/  
> application.rb which is not something I was expecting. This is  
> something I see happening even in a freshly generated rails project  
> running the default generated unit tests. I am guessing this is  
> correct behaviour but am curious as to why running a unit test on a  
> model class results in application.rb being called.

Firstly, “unit test” does not necessarily mean “model test”. Both  
verbiages are  
just a Rails opinion, as a prod to help remind you to isolate and  
decouple tests  
and business logic.

I added this to my nearest application.rb…

class ApplicationController \< ActionController::Base  
def initialize(\*args)  
raise ‘wtf?’  
super  
end

…and found that not all tests call the application. The ones that do  
are using  
test/helper\_testcase.rb:27: That’s a system that tests helper files. Did  
you  
install such a system?

If you do the same experiment, do you get that file? What’s on your  
stack trace?

> $currentsite = Site.find(1)  
> $sitefooter=$currentsite.footer  
> $sitename=$currentsite.name  
> $buttons=Button.find(:all, :order =\> “position”)

Someone is writing Perl-style Ruby here. Take out as many $ as you can,  
and add  
another point to the argument that a rewrite might be necessary.

Next, all that code runs when the ApplicationController compiles, not  
when it  
first invokes. Look up “before\_filter”, and put your (crappy) startup  
code  
inside it. Then it will only invoke when someone fetches a page - not  
necessarily when every danged test runs!

–  
Phlip
