Vijay's blog



VijayVijay
Testing modules that get included in a controller
edit Posted by Vijay on Wednesday October 22, 2008 at 09:37PM

We recently ran into a problem of testing a module that was getting included in multiple controllers. More specifically, we wanted to test a before_filter that the module was adding to each of the controller.

The code uses rspec for testing and we found out that there was no easy way for us to test the module.

The simplest way that would have worked would have been to test the controller that included the module. This was our first cut even though we were not happy with it at all since it was not testing the module in isolation. After asking around, we finally figured it out.

Here's some code

module ShowPageModule
  def self.included(base)
    base.class_eval do
      before_filter :show_title_in_view
    end
  end

  def show_title_in_view
    #do something..set some instance variable
    @fancy_page = true
  end
end

We want to test the show_title_in_view method. The idea is to create a dummy controller and include the module so that you can test the method

So here's how you do this in rspec We define the controller, add some routing code and include it in test


class FakeController < ApplicationController
  include ShowPageModule
  def test
    head :ok
  end
end

ActionController::Routing::Routes.add_route('fake_page/test', :controller => 'fake_page', :action => 'test')

describe 'ShowPageModules', ' included in a ' do
  describe FakeController do
    it "declares a before filter that sets the variable" do
      get :test
      assigns(:fancy_page).should be_true
    end
  end
end

It is that simple and you now have a very isolated unit test. The trick was to figure out the routing code.

Some caveats

  • Since routes are singleton, be very careful while adding routes
  • Also try to name your fake controller uniquely so that there are no conflicts

VijayVijay
Standup 10/2/2008: rspec running describes in order
edit Posted by Vijay on Thursday October 02, 2008 at 05:31PM

Interesting Things

  • Rspec - stubbing a class method

In Rspec you can stub a class method in a module using Module::Class:Stubs('method') similar to Mocha syntax stub! does not work for Module::Class:method in rspec

  • Rspec - running test in order

If someone knows of a way to run describes in order in rspec test, please let me know

Ask for Help

"Finder conditions in rails?"

    find(:all, :conditions => 'flag is null')  where flag is boolean

This would work in tests but not in development mode. They tried changing it to

    find(:all, :conditions => 'flag != 1')  where flag is boolean

and this would work in dev mode but not in test.

Try having boolean values in database as not null and never pass null to boolean. Make sure fixtures and development data have similar data sets.

"Selenium tests failing to load associations"

Check if you RAILS_ENV is set to test while running Selenium. There has been some cases of weird rails behavior running tests when RAILS_ENV is set to development

VijayVijay
Standup 10/01/2008: where is the to_date function in ruby/rails world?
edit Posted by Vijay on Wednesday October 01, 2008 at 05:40PM

Interesting Things

  • One of the teams was trying to reopen the Time class to make to_date public. Another pivot noticed that there is no built in to_date in Time class in ruby. ActiveSupport has a to_date method on Time class that is public. The most interesting one is that there is a private to_date that is added to Time class when you include Yaml.

  • We have git brown bag today on how to use git with the Pivotal Process.

Other stuff

  • We are looking for one more unix system administrator preferably with scalability experience to help with our infrastructure. The job description is here. Well there are obvious benefits and then there is also free breakfast :)