Wednesday, July 28, 2010

RSpec API - The Good and the Bad

The Good:

Operator Expressions : Good use of meta-programming

The Bad:

#1. Have method has bubble words and it complicates the API. It increases the noise in the code. Make the API minimal. (Bubble word - Words that does not do anything, but is used only for readability)

#2. Leaky API : render method is leaky because it depends on the argument passed in to the outer method. Basically it has parametric coupling. Developer has to *remember* the coupling in order to use the API.

Tuesday, July 27, 2010

undefined method `visit' for cucumber

Fix: Include the following configuration in features/support/env.rb

require 'webrat'
require 'webrat/core/matchers'

Webrat.configure do |config|
config.mode = :rack
config.open_error_files = false # Set to true if you want error pages to pop up in the browser
end

World(Webrat::Methods)
World(Webrat::Matchers)

This also fixes the error : "no such file to load -- action_controller/integration"

Saturday, July 24, 2010

Getting RSpec Book Rails app working on Rails 3.0 Beta 4

1. Create a new Rails app called blog, skip test unit since we will be using RSpec
rails new blog --skip-testunit

2. Edit gem file :

group :test do
gem 'rspec-rails', '>= 2.0.0.beta.10'
gem 'webrat'
gem 'selenium-client'
gem 'database_cleaner'
gem 'rspec-rails'
gem 'cucumber'
gem 'cucumber-rails'
gem 'rspec'
end

3. From app's root directory, run:

bundle install

4. rails g rspec:install

5. rails g cucumber:install

6. rails g cucumber:skeleton --rspec --webrat

7. rails g cucumber:feature

8. script/cucumber --tag @focus

9. script/cucumber -t @focus

10. rails g model movie showtime_date:date showtime_time:time
11. rake db:migrate
12. rake db:test:prepare
13. cucumber -t @focus
14. cucumber

Reference:

Rails 3, RSpec, and Cucumber

Monday, July 19, 2010

How to add a source to Ruby gems

gem sources -a http://gems.github.com

To remove a source:

gem sources -r http://gems.github.com

Run:
gem env

to see the sources that is currently in your Ruby gems.

Monday, July 12, 2010

Extremely Simple Alternative to AutoTest

I am reading the Rails 3 in Action by Yehuda. To run the sample code I customized the stakeout.rb to output the results of the test when specs get run whenever the file is changed.

1. Copy the source to a directory that is in your path:

#!/usr/bin/env ruby
if ARGV.size < 2
puts "Usage: stakeout.rb [files to watch]+"
exit 1
end

command = ARGV.shift
files = {}

ARGV.each do |arg|
Dir[arg].each { |file|
files[file] = File.mtime(file)
}
end

loop do

sleep 1

changed_file, last_changed = files.find { |file, last_changed|
File.mtime(file) > last_changed
}

if changed_file
files[changed_file] = File.mtime(changed_file)
puts "=> #{changed_file} changed, running #{command}"
puts system(command)
puts "=> done"
end

end

2. chmod +x stakeout.rb

3. To run:

stakeout.rb "spec bacon_spec.rb" **/*

This will run the spec called back_spec.rb whenever any files found recursively from the current file is changed.

Reference:

Faster TDD with Stakeout.rb

Problem Installing Sqlite3-ruby on Snow Leopard

I was getting this error when I started the server. I had installed RVM, Ruby 1.9.1, Rails 3.0 Beta 4:

.rvm/gems/ruby-1.9.1-p378/gems/sqlite3-ruby-1.3.1/lib/sqlite3/sqlite3_native.bundle: dlopen(.rvm/gems/ruby-1.9.1-p378/gems/sqlite3-ruby-1.3.1/lib/sqlite3/sqlite3_native.bundle, 9): no suitable image found. Did find: (LoadError)
.rvm/gems/ruby-1.9.1-p378/gems/sqlite3-ruby-1.3.1/lib/sqlite3/sqlite3_native.bundle: mach-o, but wrong architecture - .rvm/gems/ruby-1.9.1-p378/gems/sqlite3-ruby-1.3.1
/lib/sqlite3/sqlite3_native.bundle


Resolution:

Uninstall sqlite3-ruby (1.3.1)
1. gem uninstall sqlite3-ruby

Install sqlite3-ruby version 1.2.5
2. gem install sqlite3-ruby --version 1.2.5

If you get the error:

no such file to load -- sqlite3

when running rake db:migrate

Edit the Gemfile for the line sqlite3-ruby as :

gem 'sqlite3-ruby', :require => 'sqlite3'

Installing Ruby 1.9.2 using RVM on Snow Leopard

When I tried to install Ruby 1.9.2
rvm install 1.9.2

I got :

error: Error running 'make ', please check /Users/bparanj/.rvm/log/ruby-1.9.2-rc2/make*.log
error: There has been an error while running make. Aborting the installation.

Steps to Resolve :

1. Install libxml

curl -O ftp://xmlsoft.org/libxml2/libxml2-2.7.7.tar.gz
tar zxvf libxml2-2.7.7.tar.gz
cd libxml2-2.7.7
./configure --with-python=/System/Library/Frameworks/Python.framework/Versions/2.3/
make &
sudo make install

2. Install libxslt

curl -O ftp://xmlsoft.org/libxslt/libxslt-1.1.26.tar.gz
tar xvzf libxslt-1.1.26.tar.gz
cd libxslt-1.1.26
./configure
make
sudo make install

3.
rvm install 1.9.2-head -C --enable-shared,--with-readline-dir=/opt/local,--build=x86_64-apple-darwin10

Reference:

Installing Ruby 1.9.2 with RVM on Snow Leopard

Tips:

#1 :
rvm 1.9.2-head --default

to make it 1.9.2 when you open new terminal or after reboot.

#2 :
To revert to system installed Ruby:

rvm system --default

Thursday, July 08, 2010

uninitialized constant Mysql while running rake db:create

1. Uninstall the mysql gem

bparanj$ sudo gem uninstall mysql

Successfully uninstalled mysql-2.8.1

2. Installing the gem the right way on Snow Leopard
bparanj$ sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions. This could take a while...
Successfully installed mysql-2.8.1
1 gem installed

3. Edit the gemfile and include the line:

gem 'mysql'

4. Run bundle install from the rails project directory:

mbp2:blog bparanj$ bundle install
Fetching source index from http://rubygems.org/
Using rake (0.8.7) from system gems
Using abstract (1.0.0) from system gems
Using activesupport (3.0.0.beta4) from system gems
Using builder (2.1.2) from system gems
Using i18n (0.4.1) from system gems
Using activemodel (3.0.0.beta4) from system gems
Using erubis (2.6.6) from bundler gems
Using rack (1.1.0) from bundler gems
Using rack-mount (0.6.6) from system gems
Using rack-test (0.5.4) from system gems
Using tzinfo (0.3.22) from system gems
Using actionpack (3.0.0.beta4) from system gems
Using mime-types (1.16) from system gems
Using polyglot (0.3.1) from system gems
Using treetop (1.4.8) from system gems
Using mail (2.2.5) from system gems
Using actionmailer (3.0.0.beta4) from system gems
Using arel (0.4.0) from system gems
Using activerecord (3.0.0.beta4) from system gems
Using activeresource (3.0.0.beta4) from system gems
Using bundler (0.9.26) from system gems
Using mysql (2.8.1) from system gems
Using thor (0.13.7) from bundler gems
Using railties (3.0.0.beta4) from system gems
Using rails (3.0.0.beta4) from system gems
Using sqlite3-ruby (1.3.0) from bundler gems
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

5. rake db:create will now work
mbp2:blog bparanj$ rake db:create
(in /Users/bparanj/projects/blog)
db/test.sqlite3 already exists