Skip to content

Instantly share code, notes, and snippets.

@myronmarston
myronmarston / without_foreign_keys.rb
Created September 6, 2014 04:36
Example of how to disable foreign keys for a particular context
RSpec.shared_context "without foreign key constraints", :disable_foreign_keys do
def without_foreign_key_checks
DB.run('SET foreign_key_checks = 0;')
yield
ensure
DB.run('SET foreign_key_checks = 1;')
end
around(:example) { |ex| without_foreign_key_checks(&ex) }
end
require "bundler/inline"
gemfile do
source 'https://rubygems.org'
gem "rspec", "~> 3.10"
end
require "rspec/autorun"
RSpec.configure do |config|
class SomeEntity
def run
transaction do
mark_non_eligible
make_invoice_batch
send_batch_email
end
end
def transaction
(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
myronmarston / ways_to_use_vcr.rb
Created April 13, 2012 15:00
Ways to use VCR for a request made by a let block
# 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
foo
end
# 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

module IncrementAndDecrement
def increment(&block)
matcher = change(&block).by(1)
RSpec::Matchers::AliasedMatcher.new(matcher, lambda do |desc|
desc.gsub("changed", "incremented").gsub("change", "increment")
end)
end
def decrement(&block)
matcher = change(&block).by(-1)
@myronmarston
myronmarston / explanation.md
Last active October 22, 2020 18:16
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 = Struct.new(:name, :email)

describe User do
  subject { User.new("bob") }
  its(:name) { should == "bob" }
end
@myronmarston
myronmarston / output
Created July 28, 2012 22:16
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 https://twitter.com/garybernhardt/status/229323716
$ 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
myronmarston / output
Created May 10, 2014 07:05
Demonstration of using built in matchers from within a custom matcher
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
Failures: