Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7868 (closed defect: duplicate)

Opened 2 years ago

Last modified 2 years ago

Testing cannot be done without a database

Reported by: hitsvilleusa Assigned to: core
Priority: low Milestone: 1.x
Component: Railties Version: 1.2.1
Severity: normal Keywords:
Cc: court3nay@gmail.com

Description

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:

  1. Create a new rails project -- rails test
  2. Create a model in models/ called user.rb
  3. Have the model definition be class User; end
  4. Create a unit test in tests/unit called user_test.rb
  5. 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
    
  1. Try to run the unit test with rake test:units
  2. 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.

Change History

03/19/07 20:19:06 changed by eventualbuddha

  • status changed from new to closed.
  • resolution set to wontfix.

Well, as a workaround you could use an empty MySQL or sqlite db. That said, Rails is geared toward a pretty simple app revolving around a database. If that is not your model, then perhaps Rails is not for you. One alternative you might consider, especially if you like Ruby, is Merb.

03/21/07 01:59:47 changed by hitsvilleusa

  • priority changed from normal to low.
  • status changed from closed to reopened.
  • resolution deleted.

I trust your judgement in closing this ticket, but I honestly feel like it would be completely reasonable to want to have a rails app without a database. Rails offers so many conveniences, not to mention the prototype and scriptaculous helpers. Another good example of a legitimate app without a database would be an svn browser. There would be no reason that one would have to have a database attached to this app except if they wanted to store preferences for users, or have a wiki or bug tracking attached. Otherwise, an svn browser could be a pretty complex app, but no database. Certainly the rails conveniences would be nice to use. I realize that this would be very low on the importance scale, but it sincerely feels like a legitimate case.

03/21/07 02:00:39 changed by hitsvilleusa

i changed the status, becasuse I didn't know if the comment would be read on a closed ticket.

03/21/07 04:03:06 changed by eventualbuddha

  • status changed from reopened to closed.
  • resolution set to wontfix.

I follow the trac RSS feed, which lists comments -- even on closed tickets. I closed the ticket originally not because it's a bad idea (it's not), but because it's low priority and there's no patch. If you want to reopen the ticket with a patch it would definitely be considered. If you need any help or advice in writing a patch, please stop by #rubyonrails on freenode or email the mailing list.

03/23/07 01:17:50 changed by caboose

  • cc set to court3nay@gmail.com.

I like this idea. Take it up on the Rails Core mailing list..! :) Before you do, perhaps you could try to write a database adapter called None that just stubs everything out. It's a hack, but it might just work.

03/23/07 17:01:55 changed by bitsweat

  • status changed from closed to reopened.
  • resolution deleted.

This has come up before. The convention is to remove your database.yml. Active Record assumes it's inactive if it has no configurations.

03/23/07 17:02:07 changed by bitsweat

  • status changed from reopened to closed.
  • resolution set to duplicate.
  • milestone changed from 1.2 regressions to 1.x.

03/25/07 17:29:48 changed by adh1003

See also ticket #6795. I re-opened that one for further discussion, but there's no point re-opening this too now that it contains this link to the duplicate.