Skip to content

Instantly share code, notes, and snippets.

@ivalkeen
Created May 19, 2015 21:05
Show Gist options
  • Save ivalkeen/e3bed415e5a9b407552c to your computer and use it in GitHub Desktop.
Save ivalkeen/e3bed415e5a9b407552c to your computer and use it in GitHub Desktop.
Testing tradeoff (black-box vs implementation or refactoring vs domain logic change)
require 'minitest/autorun'
class Doctor
def initialize(last_name)
@last_name = last_name
end
def display_name
"Dr. #{@last_name}"
end
end
class Notifier
def call(doctor)
"Hello, #{doctor.display_name}"
end
end
class TestDoctor < Minitest::Test
def setup
@doctor = Doctor.new('House')
end
def test_display_name
assert_equal 'Dr. House', @doctor.display_name
end
end
class TestNotifier < Minitest::Test
def setup
@notifier = Notifier.new
end
# testing implementation details
# good for domain logic changes, bad for refactoring (introducing NameFormatter will break the test)
def test_using_mock
doctor = Minitest::Mock.new
doctor.expect :display_name, 'Dr. House'
assert_equal 'Hello, Dr. House', @notifier.call(doctor)
doctor.verify
end
# testing implementation details
# good for domain logic changes, bad for refactoring (introducing NameFormatter will break the test)
def test_real_dry_with_delegation
doctor = Doctor.new('House')
assert_equal "Hello, #{doctor.display_name}", @notifier.call(doctor)
end
# testing black-box, not implementation
# good for refactoring, bad for domain logic changes (Dr. House -> House MD)
def test_real_no_dry_black_box
doctor = Doctor.new('House')
assert_equal 'Hello, Dr. House', @notifier.call(doctor)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment