Skip to content

Instantly share code, notes, and snippets.

@kule
Created September 11, 2018 09:37
Show Gist options
  • Star 57 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kule/626c5f5a501d20c7349dc313923ef0bf to your computer and use it in GitHub Desktop.
Save kule/626c5f5a501d20c7349dc313923ef0bf to your computer and use it in GitHub Desktop.
Simplified example of how rspec works
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'colorize'
end
class MatcherInterface
def initialize(some_object)
@some_object = some_object
end
def to(matcher)
if matcher.matches?(@some_object)
puts "Test Passed".colorize(:green)
else
puts "Test Failed".colorize(:red)
end
end
def not_to(matcher)
if matcher.doesnt_match?(@some_object)
puts "Test Passed".colorize(:green)
else
puts "Test Failed".colorize(:red)
end
end
end
class HaveValue
def initialize(value)
@value = value
end
def matches?(object)
@value == object
end
def doesnt_match?(object)
@value != object
end
end
def expect(some_object)
MatcherInterface.new(some_object)
end
def have_value(value)
HaveValue.new(value)
end
foo = 1
expect(foo).to have_value(1)
expect(foo).not_to have_value(2)
bar = 100
expect(bar).to have_value(200)
expect(bar).not_to have_value(100)
@rachidcalazans
Copy link

Nice!

@timbogit
Copy link

Very nice illustration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment