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
class ApplicationController < ActionController::Base
$currentsite = Site.find(1)
$buttons=Button.find(:all, :order => “position”)
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
Replace this with your real tests.
this calls test_helper.rb which in has the following lines
ENV[“RAILS_ENV”] = “test”
require File.expand_path(File.dirname(FILE) + “/…/config/
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
I have included the static trace below for addition information:
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:infind_from_ids’
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:ingem_original_require’
require' from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/ active_support/dependencies.rb:496:inrequire’
new_constants_in' from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/ active_support/dependencies.rb:496:inrequire’
… 8 levels…
require' from ./test/unit/../test_helper.rb:3 from test/unit/poll_test.rb:1:inrequire’
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.