Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Standup 01/12/2009: require & class reloading, acts_as_fu

Interesting Things

  • Using ‘require’ explicitly interferes with class reloading in Rails

Frederick Cheung discusses this in more detail here. This might be related to the Selenium + class reloading issues some pivots have experienced in recent weeks. The alternative is to rely on Rails automagic loading or ‘require_dependency’.

  • acts_as_fu makes writing database independent tests for models is easier

Props to pivot Pat Nakajima for creating acts_as_fu.

  1. Pat Nakajima says:

    Hey, glad you like acts_as_fu. Just so you know, it’s not actually database independent. It sets up a sqlite3 database under the covers that requires no configuration. It also allows you to manually specify a different database configuration if you need one.

  2. grosser says:

    Setting up such a database by hand is not very problematic, and it keeps your code “real”, so any other developer can understand it without needing to dig into acts_as_fu (which is a very descriptive name btw)

    What i would like in a gem is something simpler: setup_ar_memory_db()
    the rest(migration+class) i would rather do by hand.

    def setup_ar_memory_db
      require 'active_record'
      ActiveRecord::Base.configurations = {"test" => {
        :adapter => "sqlite3",
        :database => ":memory:",
      ActiveRecord::Base.logger ="/dev/null")
    ActiveRecord::Schema.define(:version => 1) do
      create_table "feeds" do |t|
        t.string "feed_url"
        t.timestamp "feed_updated_at"
        t.text "feed_data"
    class Feed < ActiveRecord::Base
  3. grosser says:

    ok done:

    sudo gem install grosser-testing_database
    require 'testing_database'
    ActiveRecord::Schema.define(:version => 1) do
      create_table :users do |t|
        t.string :name
    class User < ActiveRecord::Base
  4. Pat Nakajima says:

    Hey grosser,

    You’re right, it’s definitely easy to set this stuff up manually, but after doing it enough times, I wanted a way to only setup the things I care about, and not worry about anything else. That leaves us with this:

    build_model(:users) do
    string :name

    Also, `acts_as_fu` automatically sets and unsets the class constant, meaning you have a pristine version of your ActiveRecord class every time you call `build_model`. That’s pretty nice to have when you’re manipulating the class in your tests.

    It’s certainly concise at the risk of being ambiguous, but it’s worked quite well for me in a wide variety of circumstances.

    Finally, about the name: Many ActiveRecord plugins have names like “acts_as” or “-fu”, and my project is aimed at making those plugins easier to test. Therefore, it can increase your “acts_as”-fu, or if you prefer, it can act as “fu”.

    Also, I just can’t resist a smarmy project name.

  5. grosser says:

    — automatically sets and unsets the class constant
    sounds good!

    how about a public method in acts_as_fu that does “setup_ar_memory_db” so everybody can be happy.

    –> new project name: instant_database :D

  6. grosser says:

    oh, that would be ActsAsFu.connect! silly me, did not read the docs :)
    ill give it a try then!

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *