Friday, April 27, 2012

How to comment multiple lines in Ruby

Use:
=begin
your code goes here
on multiple lines
=end

multiline comments

How to compare Time in RSpec


When you are dealing with Time the == operator will not pass the test when you compare the time objects. Quick fix is to call to_s and use == to compare the time string values. Like this:

my_timestamp.to_s.should eq("Mon Apr 09 20:56:25 UTC 2012")

Reference:

Comparing time in rspec

Monday, April 23, 2012

no such file to load -- zlib

1. Go to the directory where you extracted the Ruby package:
    Ex : cd ~/temp/ruby-1.8.7-p248/ext/zlib
2. ruby extconf.rb
3. sudo make && sudo make install

Tuesday, April 17, 2012

How to list files in MB on Linux

ls -l --block-size=1M

How to limit the number of records returned by mysqldump

How to restrict the size of mysqldump?

mysqldump --where="true LIMIT 1000" -u user-name -p db-name > exported-data.sql

How to list all the loaded gems on a server

Open a Rails console and type:

 $:.each do |x|
   p x
 end;
 p "hi"

$: variable contains the list of all gems loaded into the system. I am suppressing the output of array of those gems by doing ; p "hi" after the end.

sh: irb: not found

On Ubuntu you need to install irb :

sudo apt-get install irb

no such file to > load -- net/https when starting Rails server

Install libopenssl-ruby1.8.

Monday, April 16, 2012

How to convert ppk file to normal ssh key in Ubuntu 10.04

sudo apt-get install putty-tools
puttygen your_file.ppk -O private-openssh -o new_file_name

scp permission denied publickey

How to copy a file from a remote Ubuntu server to your machine using scp and private key


scp -i /path/to/your_private_key_file user@your_host_or_ip:/path/on/the/remote/machine/file_name .

This will copy file_name to current directory in your local machine

no such file to load -- net/https on Ubuntu 10.04

sudo apt-get install libopenssl-ruby

How to install mysql gem on Ubuntu 10.04

sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo gem install mysql
Install mysql database connector by running the above commands.

How to install Nokogiri on Ubuntu

libxml2 is missing error message is due to missing libraries. Install the pre-requisite library with:


sudo apt-get install libxslt-dev libxml2-dev

How to install curb gem on Ubuntu 10.04

sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev

How to install bson_ext on Ubuntu 10.04

Using Bundler 1.1.3 for some reason does not work. Solution:

sudo gem install bson_ext -v=1.0.1

installs the gem.

How to ssh using a given private key

ssh -i /path/to/your_private_key_file user-name@your-ip-address-here

Friday, April 13, 2012

It is recommended that your private key files are NOT accessible by others

I was getting this error : Permissions 0644 for '/home/bparanj/Downloads/my-file.ssh' are too open.
when I was ssh ing into a box.
 
Solution:
sudo chmod 600 your-private-key.ssh

Monday, April 09, 2012

Rails Recipes 3rd Edition

The recipe: Focus Your Tests with Mocking and Stubbing show how to use stubs and mocks. It violates one of the testing best practices. DO NOT mock external services. Why? Because you cannot drive the design of a third party library. For a good discussion on this topic read the "Growing Object Oriented Software Guided by Tests".

I scanned through the book, I was a bit disappointed. Lot of the recipes are just from the old book.

Tuesday, March 13, 2012

Using Stubs with Test Spy in Ruby

While I was working on my super stealth product of my company - Affiliate Tracking system, I came across a problem during testing. I had to test the cookie setting logic of my controllers. It was straightforward to test that the cookie was set for the happy path. For the alternative scenario it became tricky to test because RSpec and Rails framework did not play that well together. I even read Devise Rails plugin code to see how Jose Valim handled cookie related problems during testing. No luck. One solution I found was on Stackoverflow. How do I test cookie expiry?


# app/controllers/widget_controller.rb
...
def index
    cookies[:expiring_cookie] = { :value   => 'All that we see or seem...', 
                                  :expires => 1.hour.from_now }
end
...
# spec/controllers/widget_controller_spec.rb
...
it "sets the cookie" do
  get :index
  response.cookies['expiring_cookie'].should eq('All that we see or seem...')
                                               # is but a dream within a dream.
                                               #                - Edgar Allan Poe
end

it "sets the cookie expiration" do
  stub_cookie_jar = HashWithIndifferentAccess.new
  controller.stub(:cookies) { stub_cookie_jar }

  get :index
  expiring_cookie = stub_cookie_jar['expiring_cookie']
  expiring_cookie[:expires].to_i.should be_within(1).of(1.hour.from_now.to_i)
end

This technique is a great example of Test Spy described in Gerard Mezos book xUnit Test Patterns. Basically, you install a spy and check the results collected by the test spy in the verification phase. In this case the Hash is the Test Spy that collects data. See how the stub is used to install the spy in the SUT? It overcomes the problems and isolates the SUT from the Rails framework and allows the code to be tested easily.

In my TDD bootcamps, the topic on Stubs and Mocks generates lot of discussion. To clear confusion that surrounds the stubs and mocks, I would state : Read Martin Fowler's paper on Mocks Aren't Stubs, Stub can never fail your test, only mocks can fail your test. Using stubs in combination with a spy like this makes stubs seem like they can in fact fail your test. But only the data collected by the Test Spy decides whether the test passes or not. So the stub's main purpose is to just isolate the production code from Rails framework and allow access to the internal state of the SUT where there is no direct way to access it.