Skip to content

Instantly share code, notes, and snippets.

View fearoffish's full-sized avatar

Jamie van Dyke fearoffish

View GitHub Profile
Your Guide to Testing
This book will assume the user already knows Ruby and Ruby on Rails. It will focus on the different techniques you can use to test behaviour in Ruby/Rails applications.
Table of Contents
Chapter 1: Why Test?
Chapter 2: The Process
Chapter 3: Recipes
Fixtures or not
# Comments may or may not make sense.
class Array
def bigger?( ary )
# Check that self element is greater than comparison element
# Uses 0 if either is nil
if (self[0]||0) > (ary[0]||0)
return true
else
# if either of them don't exist as elements, comparison is bigger
# so return false
# Rails 2.1, with latest shoulda
context "POST #create" do
context "with an existing email address" do
setup do
@user = Factory.create(:user)
post :create, :email => @user.email
end
should_respond_with :redirect
@fearoffish
fearoffish / Hacked autotest.rb for running single test files
Created May 27, 2009 11:05
Quick hack of a script to replace autotest temporarily
#!/usr/bin/env ruby
require 'rubygems'
require 'directory_watcher'
puts "Watching #{Dir.pwd}/test/**/*.rb'"
dw = DirectoryWatcher.new "#{Dir.pwd}/test", :glob => '**/*.rb', :interval => 2
dw.add_observer do |*args|
args.each do |event|
case event.type
require "rubygems"
require "redis"
require 'benchmark'
count = 1000000
r = Redis.new
Benchmark.bm(10) do |x|
x.report("base") do
count.to_i.times do |i|
require "rubygems"
require "mongo_mapper"
require 'benchmark'
count = 1000000
MongoMapper.connection = Mongo::Connection.new('localhost')
MongoMapper.database = 'benchmark'
class Person
require "rubygems"
require "benchmark"
require "dm-core"
count = 1000000
class Person
include DataMapper::Resource
property :id, Serial
ruby-1.8.7-p249 (master)✕ % rake -T
/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249@retailjuice/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/core_ext/object/try.rb:29: [BUG] rb_add_method: unsupported method type (8)
ruby 1.9.2dev (2009-07-18 trunk 24186) [i386-darwin10.3.0]
-- control frame ----------
c:0057 p:---- s:0159 b:0159 l:000158 d:000158 CFUNC :alias_method
c:0056 p:0044 s:0154 b:0154 l:000153 d:000153 CLASS /Users/fearoffish/.rvm/gems/ruby-1.8.7-p249@retailjuice/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/acti
c:0055 p:0009 s:0152 b:0152 l:000151 d:000151 TOP /Users/fearoffish/.rvm/gems/ruby-1.8.7-p249@retailjuice/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/acti
c:0054 p:---- s:0150 b:0150 l
# AWESOME backtrace. Thank you!
NoMethodError (undefined method `object' for #<ArticlesController:0x1030abfa0>):
# The two lines above are the backtrace. Empty! :(
@fearoffish
fearoffish / see-all-oh-my-zsh-themes.sh
Created July 12, 2010 11:22
Open a new terminal for every oh-my-zsh theme
#!/usr/bin/env ruby
ZSHRC_FILENAME = File.expand_path("~/.zshrc")
ZSHRC_CONTENTS = File.read(ZSHRC_FILENAME)
THEMES = %x{ls ~/.oh-my-zsh/themes}.split(" ").collect {|t| t.gsub(".zsh-theme", "")}
ORIGINAL_THEME = ZSHRC_CONTENTS.scan(/export ZSH_THEME="(.*?)"/)[0].to_s
def change_theme_to(theme)
File.open(ZSHRC_FILENAME, 'w') {|f| f.write(ZSHRC_CONTENTS.gsub(/export ZSH_THEME="(.*?)"/, %[export ZSH_THEME="#{theme}"])) }
end