Skip to content

Instantly share code, notes, and snippets.

@ashmoran
ashmoran / ability_spec.rb
Created December 22, 2011 10:01
Building CanCan abilities incrementally (most edits are minor, I removed direct use of Class at some point)
require 'spec_helper'
require 'spec/environments/mongoid'
require_unless_rails_loaded 'app/models/ability'
describe Ability do
let(:user) { mock(User) }
describe ".build" do
@ashmoran
ashmoran / nested_contexts_spec.rb
Created December 16, 2011 16:45
Nesting contexts to build up a "when"
describe "#current_report" do
its(:current_report) { should be == report }
context "with a new" do
let(:new_report) { Report.make(state: new_report_state, project_date: new_report_date) }
before(:each) do
subject.reports << new_report
end
@ashmoran
ashmoran / has_many_assignment_spec.rb
Created December 15, 2011 20:02 — forked from mrkurt/Gemfile
The magical document eating has_many assignment (now incorporating the dark arts): https://github.com/mongoid/mongoid/issues/1503
require 'bundler'
Bundler.setup
require 'mongoid'
require 'rspec/autorun'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("has_many_bug")
end
class Kid
@ashmoran
ashmoran / report_state_machine_spec.rb
Created December 12, 2011 17:03
An example of specifying two linked state_machines which disable and enable each other
describe Report::ReportStateMachine do
describe "state machine" do
describe "locking state machine" do
shared_examples_for "an unlocked report" do
it { should be_report_unlocked }
describe "updating the report" do
its(:can_update_content?) { should be_false }
its(:can_check_out_report?) { should be_true }
its(:can_check_in_report?) { should be_false }
@ashmoran
ashmoran / report_locking_spec.rb
Created December 8, 2011 12:20
An example of a Cucumber-like RSpec syntax I'm using at the moment
require 'spec_helper'
require 'spec/environments/mongoid'
require 'spec/environments/capybara'
feature "Edit Report: Locking", js: true, full_backtrace: true, wip: true do
include Capybara::DSL
include AcceptanceDSL
before(:each) do
@ashmoran
ashmoran / non_local_returns.rb
Created October 31, 2011 22:34
Proof of non-local returns in Ruby
# Some of the tests for this fail, in a way that's surprising to Python users...
# Why?
class List
def initialize(*elements)
@elements = elements
@elements.each do |e|
return if e.nil?
end
end
@ashmoran
ashmoran / router.rb
Created October 6, 2011 10:05
Testing a rack-mount router
require 'rack/lobster'
module App
class Router
def initialize
@app = Rack::Mount::RouteSet.new do |set|
set.add_route ->(*args) { Rack::Lobster.new.call(*args) }, { :request_method => 'GET', :path_info => %r{^/lobster$} }, {}, :lobster
end
end
@ashmoran
ashmoran / class_typed_methods.rb
Created September 16, 2011 17:48
Class typed methods in Ruby
class Class
def define_class_typed_method(name, types, &block)
define_method(name) do |args|
args.each do |(arg, value)|
raise ArgumentError.new("Wrong class for #{arg}") unless value.is_a?(types[arg])
end
block.call(args)
end
end
end
@ashmoran
ashmoran / class_typed_methods.rb
Created September 16, 2011 17:45
Class typed methods in Ruby
class Class
def define_class_typed_method(name, types, &block)
define_method(name) do |args|
args.each do |(arg, value)|
raise ArgumentError.new("Wrong class for #{arg}") unless value.is_a?(types[arg])
end
block.call(args)
end
end
end
@ashmoran
ashmoran / webmock_weirdness_spec.rb
Created September 14, 2011 15:19
WebMock weirdness
it "satisfies: The Content-Length MUST be specified and correct according to the guidelines and rules laid out in Section 4.4, 'Message Length', of the HTTP specification" do
ascii_data = "This > O < is a snowman"
utf8_data = "This > ☃ < is a snowman"
post_request(method: ascii_data)
content_length = nil
request.with { |actual_request|
(content_length = actual_request.headers["Content-Length"].to_i) > 0
}.should have_been_requested