Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
@charusat09
Copy link

charusat09 commented Sep 14, 2018

👍

@rachidcalazans
Copy link

rachidcalazans commented Sep 15, 2018

Nice!

@timbogit
Copy link

timbogit commented Sep 15, 2018

Very nice illustration

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