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 :)

VijayVijay
Standup 09/30/2008: svn log slowness; tech talk videos
edit Posted by Vijay on Tuesday September 30, 2008 at 04:49PM

Interesting Things

  • svn log takes a long time when you have recently committed a large number such as 5000 files. Specifically, this happens when you have path based security. Here's a link from the subversion documentation that mentions this

    All of this path-checking can sometimes be quite expensive, especially in the case of svn log. When retrieving a list revisions, the server looks at every changed path in each revision and checks it for readability. If an unreadable path is discovered, then it's omitted from the list of the revision's changed paths (normally seen with the --verbose option), and the whole log message is suppressed. Needless to say, this can be time-consuming on revisions that affect a large number of files

  • Check out the new links at pivotallabs.com

    • Talks lists all the tech talks that happen here at Pivotal such as Blaine Cook's Fire Eagle talk. If you have some cool technology and would like to give a talk, contact us at techtalks@pivotallabs.com
    • Who lists all the great people who work here