Skip to content

Instantly share code, notes, and snippets.

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

Using `an_error_other_than` as an argument to `raise_error`
failing (FAILED - 1)
passing
Failures:
1) Using `an_error_other_than` as an argument to `raise_error` failing
Failure/Error: expect {
expected an error other than SyntaxError, got #<SyntaxError: SyntaxError> with backtrace:
# ./spec/raise_error_spec.rb:12:in `block (3 levels) in <top (required)>'
# A sample Gemfile
source "https://rubygems.org"
gem 'rspec', '~> 2.14.0'
@myronmarston
myronmarston / slash_vs_plus_string.rb
Last active May 20, 2016 00:27
Comparing perf of using `\` vs `+` for multiline strings
require 'benchmark/ips'
require 'allocation_stats'
def access_slashed_string
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis molestie " \
"elementum urna, a accumsan nunc euismod et. Aliquam porttitor, leo in " \
"aliquet aliquam, magna quam venenatis nulla, vel sagittis nisi nisi et " \
"nulla. Nulla quis facilisis turpis, vel blandit risus. Maecenas ut ante " \
"quis velit pretium pharetra ac rhoncus massa. Nulla quam dui, placerat " \
"eget quam vel, ultricies eleifend sem."

The reason for this is that do...end blocks bind at a different precedence level than {...} blocks. Consider this expression:

expect { foo }.to change { x }

The { x } block binds to change, and is passed as a block to that method. However, if you use do...end:

expect { foo }.to change do
# you can pass an implementation block, and use expectations in the block:
expect(MyClass).to receive(:method) do |arg_1, arg_2|
expect(arg_1).to eq(2)
expect(arg_2).to eq(3)
end
# or you can wrap any block in the `satisfy` (also aliased to `an_object_satisfying`) to turn it into a matcher:
expect(MyClass).to receive(:method).with(an_object_satisfying { |arg| arg.even? })
@myronmarston
myronmarston / Rakefile
Created December 1, 2014 20:51
dotfiles Rakefile
require 'pathname'
HOME_DIR = Pathname("~").expand_path
def symlink_files_for_dir(to_symlink)
to_symlink = to_symlink.expand_path
to_symlink_root = Pathname("./to_symlink").expand_path
to_symlink.children.each do |child|
relative = child.relative_path_from(to_symlink_root)
symlink = HOME_DIR + ".#{relative}"
@myronmarston
myronmarston / Gemfile
Last active August 29, 2015 14:08
Example for vcr/vcr#450.
# A sample Gemfile
source "https://rubygems.org"
gem 'rspec', '~> 3.0'
gem 'vcr', '~> 2.9'
gem 'webmock', '~> 1.20'
@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
@myronmarston
myronmarston / no_before_all_hooks.rb
Created August 1, 2014 15:58
How to prevent before(:all) hooks in RSpec
module PreventContextHooks
def before(*args)
disallow_context_or_all_hooks(args.first)
super
end
def after(*args)
disallow_context_or_all_hooks(args.first)
super
end