Skip to content

Instantly share code, notes, and snippets.

View nruth's full-sized avatar

Nicholas Rutherford nruth

View GitHub Profile
@nruth
nruth / example_test.erl
Created December 2, 2010 23:48
basic test spy for plain Erlang, now promoted to a full repo @ https://github.com/nruth/nspy
-module (auctioneer_tests).
-include_lib("eunit/include/eunit.hrl").
check_auctioneer_announces_item_to_room_test() ->
AuctionRoom = nspy:new(),
Auctioneer = auctioneer:start("Penguin", 20, AuctionRoom),
timer:sleep(1000),
Auctioneer ! stop,
ExpectedMessage = {starting_auction, {lot, 1, 20, "Penguin"}, "Next up lot 1, a fine Penguin, starting at 20\n"},
nspy:assert_message_received(AuctionRoom, ExpectedMessage).
#as seen in https://github.com/mooktakim/heroku_deployment
class AppTemplatesController < ApplicationController
def app
render :inline => "SUCCESS", :layout => 'master'
end
def cms
render :inline => "SUCCESS", :layout => 'cms_admin'
end
end
@nruth
nruth / gist:968289
Created May 12, 2011 10:23
controller spec describing rails log contents
describe "logger" do
before(:each) do
@old_flush = controller.logger.auto_flushing
@old_level = controller.logger.level
controller.logger.auto_flushing=0
controller.logger.level = ActiveSupport::BufferedLogger::Severity::ERROR
end
it "logs an error" do
#would prefer to test this with a test-spy / mock expectations but there's a lot of level-0 logging noise to ignore, so went with reading the log itself
@nruth
nruth / gist:1049494
Created June 27, 2011 18:50
testing devise password recovery email with email_spec and capybara / rspec request spec
require 'spec_helper'
describe "member password recovery" do
# As a member who forgot my password
# I want to recover my site access easily
#
attr_accessor :current_email_address
specify "email recovery of a new password" do
member = make_activated_member
@nruth
nruth / image_helpers.rb
Created July 6, 2011 20:13
share test helper libs between rspec and cucumber in a pluggable fashion
# an example, showing how to load for rspec and conditionally load for cucumber
# lives in whichever dir you set the other file to load
module ImageStepHelpers
def page_should_contain_image_link_for(image)
page.should have_xpath(%Q(//img[@src="#{image.url}"]))
end
def image_data_from_file(image)
image_file_path = Rails.root.join image.image.path
@nruth
nruth / cached_pages_do_not_render_flash_spec.rb
Created August 29, 2011 13:23
Introduce flash messages (notice/alert/error) to the session then hit a path and assert it does (not) render the flash messages; capybara/rspec2/rails3.0
#assumes use of capybara for request specs
require 'spec_helper'
describe "StaticPages" do
class FlashController < ApplicationController
def set_flash
flash[:notice] = params[:message]
flash[:error] = params[:message]
flash[:alert] = params[:message]
@nruth
nruth / gist:1193290
Created September 4, 2011 18:37
a few cucumber/rspec test steps for rails static page caching
Then /^a static page cache file for #{capture_model} should not exist$/ do |pickle_ref|
page = model!(pickle_ref)
page_cache_exist?(page).should be_false
end
Then /^a static page cache file for #{capture_model} should exist$/ do |pickle_ref|
page = model!(pickle_ref)
page_cache_exist?(page).should be_true
end
@nruth
nruth / rb_c_xor_file_contents_extension.rb
Created September 28, 2011 10:33
RubyInline C experimentation
# run with Piece.xor_files 'output_filepath', ['infilepath1','infilepath2']
require 'inline'
Class Piece
inline do |builder|
builder.include '<stdlib.h>'
builder.include '<stdio.h>'
builder.c_singleton <<-C_CODE
/*
rb_input_paths_array should be a ruby array of strings,
@nruth
nruth / gist:1264245
Last active August 23, 2021 01:57
Shared Capybara (or model setup) helpers for RSpec and Cucumber
# Let's assume you're driving Capybara in both RSpec request specs & Cucumber,
# for example you're using Cucumber as a design/documentation tool, and RSpec
# for the more boring integration tests.
# You don't want to duplicate your click-this-click-that helpers to e.g.
# log_in(username, password).
# You may also have model state setup code which can be shared/reused.
# Where can it go? How can it be loaded? I've been using the following approach:
#
@nruth
nruth / gist:1377390
Created November 18, 2011 18:54
A poem about OTP
Old tired programmers() ->
obviously trusted prolog,
others tried perl.
Oft they pondered() ->
of teams problems,
of telcos pains.
Onward they plodded() ->
obscure to programmers,