Thursday, January 31, 2013

rake aborted! can't convert nil into Hash


Solution Source:
Rails 3.1, rake 0.9.2.2, add this code on config/boot.rb
require 'yaml'
YAML::ENGINE.yamler = 'syck'

Monday, January 28, 2013

Useful Tools for Testing Web API


1. RSpec HTTP : https://github.com/c42/rspec-http

response.should be_http_ok
response.should be_http_created
response.should be_http_unprocessable_entity
response.should be_http_im_a_teapot
response.should have_header('Content-Type')
response.should have_header('Content-Type' => 'application/json')
response.should have_header('Content-Type' => /json/)

2. JSON Spec : https://github.com/collectiveidea/json_spec

json_spec defines five new RSpec matchers:

be_json_eql
include_json
have_json_path
have_json_type
have_json_size

3. VCR : https://github.com/myronmarston/vcr

Screencast on VCR : http://avdi.org/devblog/2011/04/11/screencast-taping-api-interactions-with-vcr/

Friday, January 25, 2013

'too many connection resets (due to HTTP session not yet started - IOError) after

This happens when you use VCR with Webmock. There is an open bug on Webmock that is causing this problem. The fix is to either downgrade the Webmock to 1.0 version or use Fakeweb.

Thursday, January 24, 2013

Using pry in test environment


Is it possible to use pry when I am running my tests? Sometimes when I am running specs for legacy code, I need a way to experiment with the existing code.


Answering my own question. Yes, it is possible. Just add binding.pry in the production code, run the specs, pry will stop at the line where you have the binding in the terminal window where you have the test running. You can also use pry-nav to use the ruby debug familiar commands step, next and continue while you debug the code.

Installing Ruby 2.0 on Mac OS 10.8.2 using RVM

1. Make sure you have the latest RVM
     rvm get head
     rvm reload


$ brew install autoconf
$ brew install automake
$ brew install libyaml

$ rvm install ruby-head

Stolen from the blog post instructions How to install Ruby 2.0 (ruby-head) with RVM

Creating a New File in a Folder in Textmate 2

Textmate 2 does not have the quick shortcut to create a file from a selected folder. Here is a workaround:

1. Select the folder where you want the new file.
2. Hit Option+Command+N keys to create a new file.
3. Add the content and save the file. It will get saved in the selected folder.

Tuesday, January 22, 2013

To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR


1. bundle open rails
      gives that error.
2. Add :
      export BUNDLER_EDITOR=mate
    to ~/.bash_profile

Thursday, January 17, 2013

How to push branch to remote

To check-in your new branch you created locally : 1. git checkout -b your_branch 2. git push -u origin your_branch

Wednesday, January 09, 2013

Configuring Moonshine to use Apache XSendFile for Rails 3.2


1. rails plugin install git://github.com/railsmachine/moonshine_xsendfile.git
2. In the Moonshine manifest file:
    configure :xsendfile => {:x_send_file_path => '/absolute/path/to/download/url'}
    recipe :xsendfile
   
    This will work even if you have subdirectories under the /are directory.
3. In production.rb, uncomment the line:
    config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache

4. In your controller:
      send_file '/absolute/path/to/download/url/file.pdf', type: 'application/pdf'

Thursday, January 03, 2013

Rails 3.2, Factory Girl


1. Include :
 
    group :development, :test do
 gem 'factory_girl_rails'
end

in Gemfile

2. bundle install
3. create factories.rb under spec folder
4. Add the factory definitions in it :

FactoryGirl.define do
 factory :user do
   email 'elinora@zepho.com'
   password 'welcome'
   primary_paypal_email 'elinora.price@zepho.com'
 end
 # product factory with a belongs_to association for the user
 factory :product do
   name 'TDD Workbook'
   price 49.99
   user
   thanks_page 'www.zepho.com/thanks.html'
   cancel_page 'www.zepho.com/cancel.html'
   sales_page 'www.zepho.com/sales_page.html'
   download_page 'www.zepho.com/download.html'  
 end
end

In this definition I have factory for user has_many :products relationship.
5. Create a support directory and include controller_macros.rb in it:
module ControllerMacros
 def login_user
   @request.env["devise.mapping"] = Devise.mappings[:user]
   user = FactoryGirl.create(:user)
   # user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
   sign_in user
 end
end
6. In spec_helper.rb add the devise helpers:
RSpec.configure do |config|
 ...
 config.include Devise::TestHelpers, :type => :controller
 config.include ControllerMacros, :type => :controller
 ...
end
7. In your controller spec, now you can do this:
 
   context 'User is logged in' do
    before do
      login_user
    end

    it 'should render index page' do
      get :index

      response.should be_success
    end
  end