Skip to content

Instantly share code, notes, and snippets.

myronmarston / ways_to_use_vcr.rb
Created Apr 13, 2012
Ways to use VCR for a request made by a let block
View ways_to_use_vcr.rb
# 1) Use VCR.use_cassette in your let block. This will use
# the cassette just for requests made by creating bar, not
# for anything else in your test.
let(:foo) { VCR.use_cassette("foo") { create(:bar) } }
it "uses foo" do
# 2) Wrap the it block that uses #foo in VCR.use_cassette.

I highly suspect that the RSpec core team all use black backgrounds in their terminals because sometimes the colors aren’t so nice on my white terminal

I certainly use a black background. I'm not sure about the other RSpec core folks. Regardless, if there are some color changes we can make that would make output look good on a larger variety of backgrounds, we'll certainly consider that (do you have some suggested changes?). In the meantime, the colors are configurable, so you can change the colors to fit your preferences on your machine. First, create a file at

View increment_and_decrement.rb
module IncrementAndDecrement
def increment(&block)
matcher = change(&block).by(1), lambda do |desc|
desc.gsub("changed", "incremented").gsub("change", "increment")
def decrement(&block)
matcher = change(&block).by(-1)
View rspec_suite_example.rb
require "bundler/inline"
gemfile do
source ''
gem "rspec", "~> 3.10"
require "rspec/autorun"
RSpec.configure do |config|
myronmarston / without_foreign_keys.rb
Created Sep 6, 2014
Example of how to disable foreign keys for a particular context
View without_foreign_keys.rb
RSpec.shared_context "without foreign key constraints", :disable_foreign_keys do
def without_foreign_key_checks'SET foreign_key_checks = 0;')
ensure'SET foreign_key_checks = 1;')
around(:example) { |ex| without_foreign_key_checks(&ex) }
myronmarston /
Last active Oct 22, 2020
Explanation for why `its` will be removed from rspec-3

its isn't core to RSpec. One the of the focuses of RSpec is on the documentation aspects of tests. Unfortunately, its often leads to documentation output that is essentially lies. Consider this spec:

User =, :email)

describe User do
  subject {"bob") }
  its(:name) { should == "bob" }
(The MIT License)
Copyright (c) 2012-2020 Myron Marston
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
myronmarston / output
Created Jul 28, 2012
Demonstration of changing the behavior of ruby's `rescue` so that it only handles shallow exceptions raised in directly called methods. Note that a bare `rescue` won't work with this, unfortunately. See
View output
$ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
$ ruby shallow_exceptions.rb
Exception handled: shallow exception
shallow_exceptions.rb:13:in `raise_shallow_exception': deep exception (StandardError)
from shallow_exceptions.rb:17:in `raise_deep_exception'
from shallow_exceptions.rb:27:in `try_handle_deep_exception'
from shallow_exceptions.rb:34:in `<main>'
myronmarston / output
Created May 10, 2014
Demonstration of using built in matchers from within a custom matcher
View output
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
Using built in matchers from custom ones
fails when the word does not end with the given letter (FAILED - 1)
fails when the word does not start with the given letter (FAILED - 2)
can match properly
View hamster_spec.rb
module Hamster
def self.vector(*args)
class Vector < ::Array
alias add <<