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.
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.
January 13, 2009 at 6:03 am
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:", }.with_indifferent_access} ActiveRecord::Base.logger = Logger.new("/dev/null") ActiveRecord::Base.establish_connection(:test) end setup_ar_memory_db ActiveRecord::Schema.define(:version => 1) do create_table "feeds" do |t| t.string "feed_url" t.timestamp "feed_updated_at" t.text "feed_data" t.timestamps end end class Feed < ActiveRecord::Base acts_as_feed endJanuary 13, 2009 at 8:55 am
ok done: http://github.com/grosser/testing_database
sudo gem install grosser-testing_database require 'testing_database' TestingDatabase.setup ActiveRecord::Schema.define(:version => 1) do create_table :users do |t| t.string :name end end class User < ActiveRecord::Base end User.create!(:name=>'EZ')January 13, 2009 at 9:20 am
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
end
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.
January 13, 2009 at 6:45 pm
– 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
January 15, 2009 at 9:06 am
oh, that would be ActsAsFu.connect! silly me, did not read the docs :)
ill give it a try then!
January 15, 2009 at 9:09 am