Skip to content

Instantly share code, notes, and snippets.

@sleepingkingstudios
Created June 20, 2017 19:11
Show Gist options
  • Save sleepingkingstudios/bdec358843a8eaf39d253edf295357fe to your computer and use it in GitHub Desktop.
Save sleepingkingstudios/bdec358843a8eaf39d253edf295357fe to your computer and use it in GitHub Desktop.
Mock Implementations of RSpec components.
module Spec::Support
class ExampleGroup
class << self
attr_reader :description
def describe description
example_group = Class.new(self)
example_group.description = description
children << example_group
example_group
end # class method describe
def inspect
return 'Spec::Support::ExampleGroup' if description.nil?
"Spec::Support::ExampleGroup(description=\"#{description}\")"
end # class method inspect
def it description, &block
end # class method it
protected
attr_writer :description
def children
@children ||= []
end # class method children
def examples
@examples = []
end # class method examples
end # eigenclass
end # class
end # module
# tmp/shared_examples_spec.rb
require 'sleeping_king_studios/tools/toolbox/mixin'
require 'rspec/sleeping_king_studios/concerns/wrap_examples'
module SharedExamples
extend SleepingKingStudios::Tools::Toolbox::Mixin
def self.included other
super
mixin_module = SleepingKingStudios::Tools::Toolbox::Mixin
return if other.is_a?(Class) || other.ancestors.include?(mixin_module)
other.extend(mixin_module)
end # method included
module ClassMethods
def include_examples description, *args, &block
shared = find_shared_examples(description)
if shared
instance_exec(*args, &shared)
instance_exec(&block) if block_given?
return
end # if
super
end # class method include_examples
def shared_examples description, &block
shared_example_groups[description] = block
end # class method shared_examples
protected
def shared_example_groups
@shared_example_groups ||= {}
end # class method shared_example_groups
private
def find_shared_examples description
ancestors.
select { |mod| mod < SharedExamples }.
each do |mod|
next unless mod.shared_example_groups.key?(description)
return mod.shared_example_groups[description]
end # each
nil
end # class method find_shared_examples
end # module
end # module
module ObjectExamples
include SharedExamples
shared_examples 'should be a' do |klass|
it { expect(instance).to be_a klass }
end # shared_examples
end # module
module StringExamples
include SharedExamples
shared_examples 'should be a string' do
it { expect(instance).to be_a String }
end # shared_examples
end # module
module GreetingExamples
include SharedExamples
include StringExamples
shared_examples 'should be a greeting' do
it { expect(instance).to start_with('Greetings') }
end # shared_examples
end # module
RSpec.describe 'default RSpec' do
shared_examples 'should be a greeting' do
it { expect(instance).to start_with('Greetings') }
end # shared_examples
subject(:instance) { 'Greetings, programs!' }
include_examples 'should be a greeting'
describe 'failing examples' do
include_examples 'should be a greeting' do
let(:instance) { 'Hello, world!' }
end # include_examples
end # describe
end # describe
RSpec.describe SharedExamples do
include ObjectExamples
include GreetingExamples
subject(:instance) { 'Greetings, programs!' }
include_examples 'should be a', String
include_examples 'should be a string'
include_examples 'should be a greeting'
describe 'failing examples' do
include_examples 'should be a greeting' do
let(:instance) { 'Hello, world!' }
end # include_examples
end # describe
describe 'wrapping examples' do
extend RSpec::SleepingKingStudios::Concerns::WrapExamples
wrap_examples 'should be a greeting'
wrap_examples 'should be a greeting' do
let(:instance) { 'Hello, world!' }
end # wrap_examples
end # describe
end # describe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment