Created
September 11, 2018 09:37
-
-
Save kule/626c5f5a501d20c7349dc313923ef0bf to your computer and use it in GitHub Desktop.
Simplified example of how rspec works
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Nice!
Very nice illustration
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍