I am trying to test my Rails application which does not have a database, and I get a lot of exceptions. I will explain:
Why would I do this?
The app that I am making is rather simple. It has a couple of controllers, and a couple of models and that is it. I do have user authentication, but it goes against an API in another app, so I don't 'have' to store any user info. The other 'data' that I present for the user is gathered from a filesystem, so again, I don't need to have a database for that. Currently this is all my app does, so I really didn't need a database...but, when I try to add a unit test to test my models which are not AR...I run into problems.
What kind of problems?
The errors initially start out with "Access denied for user 'root'@'localhost' (using password: NO)". As I address each issue, one at a time...I continue to encounter more and more issues. I had to make sure I disabled whiny_nils, I removed active_record from the config.frameworks in environment.rb. From here on out, problems begin getting more abundant. Most of the issues were around the 'assumption' that active record would exist, when in this case that was not needed.
What did I expect?
I was ok with the initial error that I got, especially when I saw that I could just exclude ActiveRecord from the framworks in environment.rb, but I would have expected the problems to end there. Also, if I had wanted to keep ActiveRecord in the stack, I would have really liked an adapter of 'none' to be acceptable. I could see a reason to want to keep it in the stack even though I wasn't using a database as my primary store. Perhaps, I had a app which just aggregated data from multiple external databases.
How to reproduce?
The easiest way to reproduce this is to do the following:
- Create a new rails project -- rails test
- Create a model in models/ called user.rb
- Have the model definition be class User; end
- Create a unit test in tests/unit called user_test.rb
- Have the test look like this:
require File.dirname(__FILE__) + '/../test_helper'
class UserTest < Test::Unit::TestCase
def test_alive
assert_equal 1, 1
end
end
- Try to run the unit test with rake test:units
- You should see some errors starting.
Other notes:
I was eventually able to get this to work, but I had to comment out all kinds of lines in the framework...one which I am pretty confident should be changed was this one:
Index: vendor/rails/railties/lib/initializer.rb
===================================================================
--- vendor/rails/railties/lib/initializer.rb (revision 5526)
+++ vendor/rails/railties/lib/initializer.rb (working copy)
@@ -209,6 +209,7 @@
end
def load_observers
+ return unless configuration.frameworks.include?(:active_record)
ActiveRecord::Base.instantiate_observers
end
All of my other changes were just to see if I could get something to work. I didn't even bother trying to get this to work with a simple controller because it didn't seem worth it at this point.