Skip to content

Instantly share code, notes, and snippets.

Avatar

Myron Marston myronmarston

View GitHub Profile
@myronmarston
myronmarston / explanation.md
Last active Oct 22, 2020
Explanation for why `its` will be removed from rspec-3
View explanation.md

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
View LICENSE
(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 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
foo
end
# 2) Wrap the it block that uses #foo in VCR.use_cassette.
@myronmarston
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 https://twitter.com/garybernhardt/status/229323716
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
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
Failures:
View increment_and_decrement.rb
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 / 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
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
View response.md

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 hamster_spec.rb
module Hamster
def self.vector(*args)
Vector.new(args)
end
class Vector < ::Array
alias add <<
end
end
View x_spec.rb
require 'yaml'
class X
def self.y
YAML.load_file("abc")
end
end
RSpec.describe X do
it 'works' do
You can’t perform that action at this time.