Posts

Expect exceptions in rspec2 test

Donnerstag, 07. April 2011, 13:31 Uhr | roberto@vasquez-angel.de |

I keep forgetting how to test exceptions with rspec2. This is an example:

  describe "something" do
    describe "exceptions" do
      it "should raise an exception" do
        expect {
          # Code that throws an exception
        }.to raise_error
      end

The ultimate cheat sheet repository

Donnerstag, 07. April 2011, 08:11 Uhr | roberto@vasquez-angel.de |

cheat.errtheblog.com

Capistrano and bundle config

Montag, 04. April 2011, 16:40 Uhr | roberto@vasquez-angel.de |

When you use capistrano and you need a custom bundler config in your Rails app, you have the problem, that it gets lost after each update.

The solution to this problem is to move the .bundle folder in your rails app, to the shared folder in your capistrano environment. Then you tell capistrano to link the .bundle to the shared folder after each update.

  • First, move your Rails.root/.bundle to ../../shared/.bundle
  • Add a rake task to Rails.root/config/deploy.rb:
namespace(:bundle) do
  desc "Symlinks your machine specific bundle to your rails app"
  task :symlink, :roles => :app do
    run <<-CMD
      ln -nfs #{shared_path}/.bundle #{release_path}/.bundle
    CMD
  end
end
  • tell capistrano to invoke the bundle:symlink task after symlinking your freshly deployed realase. In your Rails.root/config/deploy.rb:
after "deploy:symlink", "bundle:symlink"

W3C HTML Validator

Montag, 04. April 2011, 01:30 Uhr | roberto@vasquez-angel.de |

The unicorn HTML validator is at W3C Unicorn HTML Validator

What's next?

Montag, 04. April 2011, 01:20 Uhr | roberto@vasquez-angel.de |

After having tried wordpress and some other blog systems, I finally took the time to write my own blogging software. Of course, I used Ruby on Rails to write it.

This Blog runs on DomainFactory (kick-ass RoR Hosting in Germany) and uses:

I’ll add support for comments and post categories (tags) asap!

Devise: Testing controllers and requests that need authentication with rspec2

Montag, 04. April 2011, 01:07 Uhr | roberto@vasquez-angel.de |

Imagine you have been happily writing your controller test and then, you have to add authentication to your application using devise.

Assume you have a posts controller with following test:

require 'spec_helper'

describe PostsController do
  def mock_post(stubs={})
    @mock_post ||= mock_model(Post, stubs).as_null_object
  end

  describe "GET index" do
    it "assigns all posts as @posts" do
      Post.stub(:all) { [mock_post] }
      get :index
      assigns(:posts).should eq([mock_post])
    end
  end

  describe "GET show" do
    it "assigns the requested post as @post" do
      Post.stub(:find).with("37") { mock_post }
      get :show, :id => "37"
      assigns(:post).should be(mock_post)
    end
  end
end  

First, you’ll have to add the Devise TestHelpers. Add a new file spec/support/devise.rb:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

You’ll need a factory for the admin. I use factory girl. Add the admin factory to Rails.root/spec/factories.rb:

Factory.define(:admin) do |admin|
  admin.email    "admin@example.com"
  admin.password "foobar"
end

Then you throw all your controller tests into a new describe block, that signins in your admin before each test:

describe PostsController do
  describe "as a signed in admin" do
    before(:each) do
      admin = Factory(:admin)
      sign_in admin
    end
    
    def mock_post(stubs={})
      @mock_post ||= mock_model(Post, stubs).as_null_object
    end

    describe "GET index" do
      it "assigns all posts as @posts" do
        Post.stub(:all) { [mock_post] }
        get :index
        assigns(:posts).should eq([mock_post])
      end
    end

    describe "GET show" do
      it "assigns the requested post as @post" do
        Post.stub(:find).with("37") { mock_post }
        get :show, :id => "37"
        assigns(:post).should be(mock_post)
      end
    end
  end
end

Customize the request specs. Before:

require 'spec_helper'

describe "NewsItems" do
  describe "GET /news_items" do
    it "works! (now write some real specs)" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get news_items_path
      response.status.should be(200)
    end
  end
end

and with devise integration:

require 'spec_helper'

describe "NewsItems" do
  context "as logged in admin" do
    before(:each) do
      admin = Factory(:admin)
      get new_admin_session_path
      fill_in :email,    :with => admin.email
      fill_in :password, :with => admin.password
      click_button
    end
    
    describe "GET /news_items" do
      it "works! (now write some real specs)" do
        # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
        get news_items_path
        response.status.should be(200)
      end
    end
  end
end

Rake task to create a new devise user

Montag, 04. April 2011, 01:07 Uhr | roberto@vasquez-angel.de |
namespace :devise do
  desc "Create admin"
  task :create_admin, :email, :password, :needs => :environment do |t, args|
    if a = Admin.create(:email => args.email, :password => args.password)
      puts "Created admin"
    else 
      puts a.errors
    end    
  end
end  

Usage:

$> rake devise:create_admin["user@example.com","password"]

Markdown online tester and cheat sheet

Montag, 04. April 2011, 01:07 Uhr | roberto@vasquez-angel.de |

Dingus

Add a new role to your site with devise

Montag, 04. April 2011, 01:06 Uhr | roberto@vasquez-angel.de |

Adding a new role (i.e. “Admin”) is easy:

$> rails generate devise Admin

Don’t forget to migrate:

$> rake db:migrate

Then you can check for the admin in your controllers:

class BackendController < ApplicationController
  before_filter :authenticate_admin!
end 

Installing Devise

Montag, 04. April 2011, 01:06 Uhr | roberto@vasquez-angel.de |
  • Add the gem to your Gemfile:
gem 'devise'
  • Install your Bundle:
$> bundle install
  • Run the generator:
$> rails generate devise:install

This will generate a initialiter in your config/initializers folder. You should check the options in config/initializers/devise.rb

  • Add default URL options to your mailer configuration in the environments files:

environments/development.rb:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

environments/production.rb:

config.action_mailer.default_url_options = { :host => 'blog.robotex.de' }

svn:ignore für Rails Projekte

Montag, 04. April 2011, 01:06 Uhr | roberto@vasquez-angel.de |

.svnignore Datei im Rails.root erstellen:

.bundle
.project
*.swp
*~
webrat.log
db/*.sqlite3
log/*
tmp/*
doc/api/*
doc/app/*

und danach die Datei “scharf” schalten:

svn -R propset svn:ignore -F .svnignore .