Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active July 6, 2020 08:51
Show Gist options
  • Save krisleech/7c292bc1124a19715f401fc144f6982d to your computer and use it in GitHub Desktop.
Save krisleech/7c292bc1124a19715f401fc144f6982d to your computer and use it in GitHub Desktop.
RSpec gotcha
require "bundler/inline"
gemfile(false) do
source "https://rubygems.org"
gem "rspec"
gem "pry-byebug"
end
require 'rspec/autorun'
RSpec.shared_context 'foo' do
let(:name) { 'ONE' }
it 'works' do
expect(name).to eq('ONE')
end
end
RSpec.shared_context 'bar' do
let(:name) { 'TWO' }
it 'works' do
expect(name).to eq('TWO')
end
end
RSpec.describe 'test' do
# FAILS
include_examples 'foo' # fails (name is "TWO")
include_examples 'bar'
# FAILS
include_context 'foo' # fails (name is "TWO")
include_context 'bar'
# WORKS
# it_behaves_like 'foo' # passes
# it_behaves_like 'bar'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment