Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Standup 10/15/2008: this_method; dynamically creating tables for testing

Joe Moore
Wednesday, October 15, 2008
  • Where am I? — Ever need to find the name of the method you are currently within? Here’s a this_method method! The magic is in the REGEX, of course.
module Kernel
  private
  def this_method
    caller[0] =~ /`([^']*)'/ and $1
  end
end
  • One project wanted to test a very ActiveRecord-specific Module in an isolated, generic way. After spending time researching techniques of mocking and stubbing the many, many ActiveRecord methods that would be touched, they decided to just dynamically create an ActiveRecord and a DB Table for it on the fly! They even used single table inheritance (STI)
  describe "MyMagicModule Mixin" do
    before(:all) do
      ActiveRecord::Base.connection.create_table "some_base_models",
                                                   :force => true do |t|
        t.string   "name"
        t.string   "type"
        t.integer  "some_model_b_id", :limit => 11
      end
    end

    after(:all) do
      ActiveRecord::Base.connection.drop_table "some_base_models"
    end


    class SomeBaseModel < ActiveRecord::Base;end

    class SomeModelA < SomeBaseModel
      include MyMagicModule

      belongs_to: :some_model_b
    end

    class SomeModelB < SomeBaseModel
      include MyMagicModule
    end

    it 'should use special belongs_to stuff from MyMagicModule' do
       model_a = SomeModelA.create!(
                        :name=> "Model A",
                        :some_model_be => SomeModelB.create!(:name => "Model B"))
       # test the functionality from MyMagicModule
    end
  end
  • 0 Shares
  • Share on Facebook
  • Share on Twitter

3 Comments

  1. J. Ryan Sobol says:

    “Where Am I?” – seems *very* useful when debugging meta-programming soup.

    October 17, 2008 at 5:31 pm

  2. Pat Nakajima says:

    Hey, for dynamically creating activerecord models, take a look at my acts_as_fu project: [github.com/nakajima/acts_as_fu](http://github.com/nakajima/acts_as_fu). It does pretty much the same thing you showed, just a bit more concisely.

    October 27, 2008 at 2:11 am

  3. Joe Moore says:

    @Pat — I just used [acts_as_fu](http://github.com/nakajima/acts_as_fu) on a new project and it is the new hotness! Thanks!

    January 10, 2009 at 1:32 am

Add New Comment Cancel reply

Your email address will not be published.

Joe Moore

Joe Moore
New York

Recent Posts

  • How We Use tmux for Remote Pair Programming
  • Integrating Remote Developers: Intuitive, Flexible Video Conferencing
  • Pair Programming Matrix
Subscribe to Joe's Feed

Author Topics

agile (117)
pair programming (6)
remote (4)
remote pair programming (3)
tmux (1)
android (10)
mobile (10)
robolectric (4)
java (4)
c2dm (2)
testing (8)
apple (1)
ipad (1)
pivotal tracker (6)
sf.tug (2)
desert (2)
ruby on rails (13)
javascript (2)
capistrano (1)
palmpre (1)
active record (3)
activerecord (6)
mysql (1)
rubymine (1)
fun (4)
acts_as_fu (1)
rspec (2)
xp (1)
flash (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >