use rspec filter to speed up tests

Rspec 2 introduce a very efficient way to test only one test or one test suit, it's filter_run.

You should first add filter_run in rspec/spec_helper.rb

config.filter_run :focus => true

Then you can tell rspec to test only one test you are focused by

it "should focus now", :focus => true do
  ...
end

rspec will only test this spec, :focus => true can be applied on describe/context as well.

One problem is that if there is no :focus => true on your tests, rspec will do nothing, but most of time we are expecting to test all specs if no focus is true, so you should add a line to spec_helper as well.

config.run_all_when_everything_filtered = true

As the name implies, rspec will test all specs if no focus filter.

Another you may interest that you can also define filter_run_excluding

config.filter_run_excluding :slow => true

rspec will run all specs except what specs are marked as slow.

Posted in  Rspec


rubykaigi presentation

My presentation in RubyKaigin 2011 today.

and the video is here: http://www.ustream.tv/recorded/16051491

Posted in  Presentation Ruby Rails


beijing ruby线下活动

周日在北京的ruby线下活动的ppt

Posted in  Presentation


Upgrade Mongoid - Multiple databases

My recent post Use different mongodb instances in mongoid tells you how to use multiple databases, it looks good, but mongoid began to support multiple databases itself from mongoid.2.0.0.rc.1, much better than my hack.

It's really easy to use, first, you should define multiple databases in mongoid.yml like

development:
  <<: *defaults
  host: localhost
  database: main_mongo_instance
  databases:
    other_mongo_instance_name:
      database: other_mongo_instance
      host: localhost

As you seen, besides the common database param, I have defined a new param databases, you should define the mongo instance name with database and host name, and of course, you can define as many mongo instances as you need.

Then, you can choose which mongo instance to use in mongoid model.

class User
  include Mongoid::Document

  set_database :other_mongo_instance_name
end

set_database method tells mongoid that the model will use another mongo instance instead of the main mongo instance, here we use the name other_mongo_instance_name which should exactly be the same with the name defined in mongoid.yml. If you don't say anything, it will use the main_mongo_instance.

So all the users data will be stored to other_mongo_instance_name, and the other data will be stored to main_mongo_instance. Great!

Posted in  mongoid Ruby


Upgrade Mongoid - update_attribute

Before mongoid 2.0.0.rc.6, there is no update_attribute method for Mongoid::Document, it makes me unhappy. As in ActiveRecord world, I always use update_attribute to change one attribute and use update_attributes to change two or more attributes.

It's a good news that mongoid introduces the update_attribute method from 2.0.0.rc.6, now I can follow my practice in mongoid.

post.update_attribute(:title => "New Post")

post.update_attributes(:title => "New Post", :body => "New Body")

Posted in  mongoid Ruby


Fork me on GitHub