Skip to content

Announcing Mainline

Mainline is a rails plugin which exposes your rails app via webrick to allow
testing with browser automators such as Selenium or Watir. Mainline allows
your rails actions to run in the same transaction as your unit tests so you
can use fixtures, factories, or whatever.

Basically you can now test selenium in your same transaction and don’t have to worry about rolling back your fixtures or factories.

Grab it from Github
Bug Reports at Lighthouse
Docs at RDocul.us

Making your Plugin or Gem configurable

Recently I added a configuration mechanism to Webrat. It was surprisingly easy, and mainly copied from rails core. I would suggest adding somthing like this to any plugin that has more than a few features or ones that users have asked to have turned off.

First off you’re going to have to create the actual configuration object. There are a few good ways to do this. One is to use a config module, another is to create a configuration object that is accessible via a singleton method.

I’m going to go with the second one, a configuration object.

Toss this one in lib/configuration.rb (A simplification of Code | RDoc)

module Plugin

  # Configures Plugin.
  def self.configure(configuration = Plugin::Configuration.new)
    yield configuration if block_given?
    @@configuration = configuration
  end

  def self.configuration # :nodoc:
    @@configuration ||= Plugin::Configuration.new
  end

  # Plugin can be configured using the Plugin.configure method. For example:
  #
  #   Plugin.configure do |config|
  #     config.show_whiny_errors = false
  #   end
  class Configuration

    # Should whiny error messages be shown?
    attr_writer :show_whiny_errors

    def initialize # :nodoc:
      # set your defaults in here
      self.show_whiny_errors = true
      # put as much as you want in here
    end

    # some syntactic sugar for you, the coder
    def show_whiny_errors? #:nodoc:
      @show_whiny_erorrs ? true : false
    end

  end

end

Okay, now we need to test the config object itself. This is why it’s nice to make an object just to house the config, it’s easy to test. What do we test? Well defaults and accessors for two!

(The following lifted from Code) (sorry this is in rspec, it’s not hard to do in test::unit)

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe Plugin::Configuration do
  # define matchers for testing
  predicate_matchers[:show_whiny_errors] = :show_whiny_errors?

  it 'should show whiny errors by default' do
    config = Plugin::Configuration.new
    config.should show_whiny_errors?
  end

  it 'should be configurable with a block' do
    Plugin.configure do |config|
      config.show_whiny_errors = false
    end

    config = Plugin.configuration
    config.should_not show_whiny_errors?
  end

end

Now we need to do some stuff to make it nicer for our other users to test. Put the following in your test_helper or spec helper. It will allow you to clear your config after each test. Nice to have to to avoid messy test issues.

(The following lifted from Code)

module Plugin
  @@previous_config = nil

  def self.cache_config_for_test
    @@previous_config = Plugin.configuration.clone
  end

  def self.reset_for_test
    @@configuration = @@previous_config if @@previous_config
  end
end

# configure your test runner / spec runner to always clear the config
Spec::Runner.configure do |config|

  config.before :each do
    Plugin.cache_config_for_test
  end

  config.after :each do
    Plugin.reset_for_test
  end
end

This last bit is somewhat hard to do in test::unit as it is harder to hook into the setup (you only get one in the call chain). I’m willing to take some help on cleaning this one up for test::unit. Currently I have just been putting it in the setup / teardown for each test file.

Finally you need to make use of this in tests, fortunately that is quite easy. Just:

describe SomeObject do
    it 'it shouldn't do it when whiny nils are off' do
      Plugin.configure do |config|
        config.show_whiny_errors = false
      end
      object.should_not_receive(:log)

      object.do_somthing_that_usually_complains
    end
end

Finally, anywhere in your plugin that you think somthing is whiny, just check the config before using it like this:

log("you should really fix this") if Plugin.configuration.show_whiny_errors?

chicken and egg

I can’t believe this still hapens. Unzip software that is zipped. I was trying to unzip a file off of the web to install on my palm and they had zipped the 15k zip prc file to save those few extra bytes on the web making it impossible to actually download and use the software without a full computer.

Ugh.

generator_missing plugin release

I just posted my initial cut of my generator_missing plugin. This plugin is meant to be a repository of additional generators to help workflow. The current ones are:

  • Library: Generates a non active record ruby object and a test class.
    • lib/library_name.rb
    • test/unit/library_name_test.rb
  • Module: Generates a basic module under lib and its tests.
    • lib/module_name.rb
    • test/unit/module_name_test.rb, with a mock class that mixes in the library
  • Base Generator: Generates a generator (mmm meta) with a template, usage and manifest.
    • lib/generators/generator_name/generator_name_generator.rb
    • lib/generators/generator_name/USAGE
    • lib/generators/generator_name/templates

The plugin is at: http://github.com/gaffo/generator_missing / git://github.com/gaffo/generator_missing.git
Lighthouse at http://gaffo.lighthouseapp.com/projects/21321-generator_missing/overview.

I am currently taking both suggestions and pull requests for generators that should be added.

RDocul.us launch

I was tired of not having up to date docs for the rails gems and plugins that I use, so I created RDocul.us. Bascially, anyone is free to submit any github based rails plugin or gem and it will be kept up to date on RDocul.us. Once it’s approved it will be automatically updated within four hours of any commit to github.

Currently there is a delay for as I am manually vetting and configuring the libraries. Unfortunately not everyone is creating the .documentation file or using the same rake task (rdoc, doc, docs) or doc directory (rdoc, doc, docs) so I have to give it a quick once over.

Please let check it out and let me know what you think.

testing named scopes

Named scopes are a nice feature that came out in rails 2.1, however, testing them is not very obvious.

Say we have a named scope in our member object which looks like this:

class Member < ActiveRecord::Base
  named_scope :active, {:conditions => {:status => Member::STATUS_ACTIVE}}
end

There are 2 things we need to do to test them.

First would be to make sure that the conditions are the same as we think they are. This one is open for debate a bit as it is somewhat tying the implementation directly to the test.

Second, we actually have to USE the scope. Just calling Member.active does not actually use the scope. This is because named scopes can be cascaded. If I had recent scope on a member that only showed me those that signed up recently, I could actually do Member.active.recent, and it would not execute against database, it would just nest the conditions.

So how do I use the scope? Use any collection method on them. I like size, but I’m completely open to suggestions. So below is how I am testing named scopes.

class MemberTest < ActiveRecord::TestCase
  context 'named scope active' do
    setup do
      @scoped_find = Member.active
    end

    should "have condition active" do
      expected_conditions  = {:conditions => ["member.status == #{Member::STATUS_ACTIVE}"]}
      assert_equal expected_conditions, @scoped_find.proxy_options
    end

    should "have results" do
      assert_not_nil(scoped.active.size)
    end
  end
end

The first test does part 1, the 2nd, part 2.

Mysql issue with rails and antivirus on windows


abstract_adapter.rb:150:in log': Mysql::Error: Can't create/write to file 'C:\MySQL5\tmp\#sql_190_0.MYI' (Errcode: 13)

I had been getting this issue quite a bit recently. The cause actually turned out to be a conflict between McAfee and MySQL. What was happening is that McAfee scans any file that is recently written to, especially those in tmp directories. McAfee reading the file causes the above issue to MySQL. The fix is 2 fold. First, if you have not already, move the location of the MySQL tmp file. You can do this by editing my.ini in your MySQL directory. For Example:

tmpdir="C:/Program Files/MySQL/tmp/"

You may also have to add an exception to your antivirus so that it will no longer scan this file. I had to do this because I was using corporate antivirus. You may have to get your Sysadmin to do this.

Here is a post on mysql’s forums describing this issue as well

testing protected and private methods in ruby

When I was looking for how to test protected an private methods in ruby on the net, I found many sites arguing whether you should, and several methods for doing so. I am of the opinion that if your method contains any logic at all, it should have a test. Some examples of what I consider logic:

Object:

class User
  validates_presence_of :first_name, :last_name, :company_id
  belongs_to :company
  def full_name
    "#{last_name}, #{first_name}"
  end
  def company_name
    company.name
  end
end

Test:

class UserTest < Test::Unit
  def test_full_name
    assert_equal("Gaffney, Mike", User.new(:first_name => "Mike", :last_name => "Gaffney"))
  end
  def test_company_name
    company = Company.new(:name => "CompanyName")
    user = User.new(:company => company)
    assert_equal("CompanyName", user.company_name)
  end
end

This may seem like overkill but when many people are looking at the code it greatly helps communicate intention to others.

Testing:

I also found several methods to test protected and private methods in ruby, so I will cover the pluses and minuses the ones I know of.

Lets say we have a User class and an unassociated comments class. Comments are only attributed to a poster’s full name. There is no direct ID link between comments and users.

class User
  validates_presence_of :first_name, :last_name
  def find_comments
    Comments.find(:all, :conditions => {:poster => full_name})
  end
  protected
  def full_name
    "#{last_name}, #{first_name}"
  end
end

Here are the approaches I’m going to use to test this:

  • Using a Mock to open access restrictions
  • Make single (protected/private) methods public on the tested class.
  • Make all (protected/private) methods public on the tested class.
  • Hybrid Mock Approach
  • Using instance_eval and send
  • send

Using Mocks:
Mocks should be pretty familiar to most developers. Basically you create a class or subclass that the test will use. In our case we are simply opening the access restrictions with a subclass. Our test class looks like this:

class MockUser < User
  def full_name
    super
  end
end
class UserTest < Test::Unit
  def test_full_name
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

Plusses:

  • Simple and familiar to most developers from many languages

Minuses:

  • Can’t test private methods (subclass doesn’t have access).
  • With complicated classes you end up with quite a few mocks for all of the different cases. Managing the mocks becomes tedious.

Make single methods public:
Next we will make use of the runtime nature of ruby to change a function from protected to public. This also works for private methods:

class UserTest < Test::Unit
  def test_full_name
    User.send(:public, :first_name)
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

or

class User
    public :first_name
end
class UserTest < Test::Unit
  def test_full_name
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

Plusses:

  • Lets us directly access the private or protected function.

Minuses:

  • This pollutes the default namespace for all of the other tests. Remember that tests can (and should be able to) run in random order. Take for example:
    1. Full Name is a public function that some other objects use.
    2. We make this function into a protected one and use this method to make it public for testing.
    3. If this new test runs before the tests for the objects using the old public full_name, full_name will still be public even though it is actually protected.
    4. The other tests do not fail but the code will fail during runtime.

Make all methods public:
To save us some time on a big class, we can make all private and protected methods public:

class User
  public *protected_methods.collect(&amp;amp;amp;:to_sym)
  public *private_methods.collect(&amp;amp;amp;:to_sym)
end
class UserTest
  def test_full_name
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

or

class UserTest < Test::Unit
  def test_full_name
    User.send(:public, *MyClass.protected_instance_methods)
    User.send(:public, *MyClass.private_instance_methods)
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

This has the same issues as above but can be done one to make large classes easy to test, especially using the first method.

Hybrid Approach:
This works like the mock example and the previous example combined:

class AllAccessUser
  public *protected_methods.collect(&amp;amp;amp:to_sym)
end
class UserTest < Test::Unit
  def test_full_name
    user = AllAccessUser.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

This is easy like the previous but does not pollute the namespace. However it cannot be used for private methods.

Using send
Now we will use the built in reflection method to call the protected/private method on the class. This makes use of the fact that protected and private in ruby aren’t really like protected and private in other languages.

class UserTest < Test::Unit
  def test_full_name
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.send(:full_name))
  end
end

Plusses:

  • Doesn’t pollute the namespace.
  • All code is right in the test.
  • Works for protected and private.

Minuses:

  • Some people don’t like using send

Summary:
Testing protected and private methods should be done, and ruby makes it much easier than some other languages. My preferred technique is the final one of using send. It is slightly less readable but keeps everything contained in one location.

hiding production database info from source control with capistrano

Whether you are working on an open source app or an enterprise app, it is a good idea to keep the production database connection information out of source control. To do so, add the following to your deploy.rb file:

task :overwrite_database_yml_file do
  run "[ -f /var/rails/#{application}/database.yml ] &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; cp -f /var/rails/#{application}/database.yml #{latest_release}/config/ || echo 'database.yml was not overwritten'"
end
after "deploy:update_code", :overwrite_database_yml_file

And place a database.yml file containing only your production connection information in the same directory as current and shared (eg the deploy_to directory). In fact I typically take the production information out of the config/database.yml entirely.

When deploy runs, if it finds a database.yml file in the deploy_to directory, it will copy it into config before migrating or starting the application. You can use this task to copy other configuration files that you want to keep out of source control as well, but I think its a good idea to only have the database connect info and host names change between deployments.

new has_a block

Pretty simple post, but any ruby object can be post conifged in a line with somthing like:

User.new(:name => “tony”){|u| u.save}

which is great for active record tests.

Or in a bigger example:

@trip = Trip.new{|t| t.save!}
@invite1 = Invitation.new(:user => users(:aaron),
                                   :status => Invitation::STATUS_MAYBE){|i| i.save}
@invite1 = Invitation.new(:user => users(:aaron),
                                   :status => Invitation::STATUS_MAYBE){|i| i.save}