Skip to content

Instantly share code, notes, and snippets.

@searls
Last active June 6, 2023 00:01
Show Gist options
  • Save searls/1f6fb63d81311001bf116f26ea8dfbd8 to your computer and use it in GitHub Desktop.
Save searls/1f6fb63d81311001bf116f26ea8dfbd8 to your computer and use it in GitHub Desktop.
# typed: true
# This script prints:
#
# <-- Stubbing based on kwarg comparison with a TYPED method
# Should be tim: "jan greeting"
# Should be jan: "jan greeting"
# Should be nil: "jan greeting"
# <-- Stubbing based on kwarg comparison with an UNTYPED method
# Should be tim: "tim greeting"
# Should be jan: "jan greeting"
# Should be nil: nil
require "sorbet-runtime"
require "mocktail"
T::Configuration.default_checked_level = :never
class Typed
extend T::Sig
sig { params(name: String).returns(String) }
def greet(name:)
"Hello, #{name}"
end
end
typed = Mocktail.of(Typed)
Mocktail.stubs { typed.greet(name: "Tim") }.with { "tim greeting" }
Mocktail.stubs { typed.greet(name: "Jan") }.with { "jan greeting" }
puts "<-- Stubbing based on kwarg comparison with a TYPED method"
puts "Should be tim: " + typed.greet(name: "Tim").inspect
puts "Should be jan: " + typed.greet(name: "Jan").inspect
puts "Should be nil: " + typed.greet(name: "Roy").inspect
class Untyped
def greet(name:)
"Hello, #{name}"
end
end
untyped = Mocktail.of(Untyped)
Mocktail.stubs { untyped.greet(name: "Tim") }.with { "tim greeting" }
Mocktail.stubs { untyped.greet(name: "Jan") }.with { "jan greeting" }
puts "\n<-- Stubbing based on kwarg comparison with an UNTYPED method"
puts "Should be tim: " + untyped.greet(name: "Tim").inspect
puts "Should be jan: " + untyped.greet(name: "Jan").inspect
puts "Should be nil: " + untyped.greet(name: "Roy").inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment