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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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?> 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")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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---