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:
base.rb:1267:in `find_one': Couldn't find Site with ID=1
... 8 levels...
from test/unit/poll_test.rb:1:in `require'
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.