Module: Mocktail

Extended by:
DSL, T::Sig
Defined in:
lib/mocktail/value/cabinet.rb,
lib/mocktail.rb,
lib/mocktail/dsl.rb,
lib/mocktail/debug.rb,
lib/mocktail/typed.rb,
lib/mocktail/errors.rb,
lib/mocktail/version.rb,
lib/mocktail/matchers.rb,
lib/mocktail/share/bind.rb,
lib/mocktail/value/call.rb,
lib/mocktail/resets_state.rb,
lib/mocktail/value/double.rb,
lib/mocktail/explains_nils.rb,
lib/mocktail/imitates_type.rb,
lib/mocktail/replaces_next.rb,
lib/mocktail/replaces_type.rb,
lib/mocktail/verifies_call.rb,
lib/mocktail/collects_calls.rb,
lib/mocktail/explains_thing.rb,
lib/mocktail/value/stubbing.rb,
lib/mocktail/sorbet/mocktail.rb,
lib/mocktail/value/signature.rb,
lib/mocktail/value/top_shelf.rb,
lib/mocktail/handles_dry_call.rb,
lib/mocktail/registers_matcher.rb,
lib/mocktail/value/demo_config.rb,
lib/mocktail/value/double_data.rb,
lib/mocktail/value/explanation.rb,
lib/mocktail/registers_stubbing.rb,
lib/mocktail/sorbet/mocktail/dsl.rb,
lib/mocktail/handles_dry_new_call.rb,
lib/mocktail/initializes_mocktail.rb,
lib/mocktail/matcher_presentation.rb,
lib/mocktail/records_demonstration.rb,
lib/mocktail/sorbet/mocktail/debug.rb,
lib/mocktail/sorbet/mocktail/typed.rb,
lib/mocktail/share/cleans_backtrace.rb,
lib/mocktail/share/stringifies_call.rb,
lib/mocktail/sorbet/mocktail/errors.rb,
lib/mocktail/value/explanation_data.rb,
lib/mocktail/value/fake_method_data.rb,
lib/mocktail/value/matcher_registry.rb,
lib/mocktail/value/type_replacement.rb,
lib/mocktail/sorbet/mocktail/version.rb,
lib/mocktail/value/unsatisfying_call.rb,
lib/mocktail/share/creates_identifier.rb,
lib/mocktail/simulates_argument_error.rb,
lib/mocktail/sorbet/mocktail/matchers.rb,
lib/mocktail/value/no_explanation_data.rb,
lib/mocktail/handles_dry_call/logs_call.rb,
lib/mocktail/imitates_type/makes_double.rb,
lib/mocktail/sorbet/mocktail/share/bind.rb,
lib/mocktail/sorbet/mocktail/value/call.rb,
lib/mocktail/replaces_type/redefines_new.rb,
lib/mocktail/value/type_replacement_data.rb,
lib/mocktail/raises_neato_no_method_error.rb,
lib/mocktail/sorbet/mocktail/resets_state.rb,
lib/mocktail/sorbet/mocktail/value/double.rb,
lib/mocktail/stringifies_method_signature.rb,
lib/mocktail/share/stringifies_method_name.rb,
lib/mocktail/sorbet/mocktail/explains_nils.rb,
lib/mocktail/sorbet/mocktail/imitates_type.rb,
lib/mocktail/sorbet/mocktail/replaces_next.rb,
lib/mocktail/sorbet/mocktail/replaces_type.rb,
lib/mocktail/sorbet/mocktail/value/cabinet.rb,
lib/mocktail/sorbet/mocktail/verifies_call.rb,
lib/mocktail/sorbet/mocktail/collects_calls.rb,
lib/mocktail/sorbet/mocktail/explains_thing.rb,
lib/mocktail/sorbet/mocktail/value/stubbing.rb,
lib/mocktail/share/determines_matching_calls.rb,
lib/mocktail/sorbet/mocktail/value/signature.rb,
lib/mocktail/sorbet/mocktail/value/top_shelf.rb,
lib/mocktail/grabs_original_method_parameters.rb,
lib/mocktail/sorbet/mocktail/handles_dry_call.rb,
lib/mocktail/sorbet/mocktail/registers_matcher.rb,
lib/mocktail/sorbet/mocktail/value/demo_config.rb,
lib/mocktail/sorbet/mocktail/value/double_data.rb,
lib/mocktail/sorbet/mocktail/value/explanation.rb,
lib/mocktail/handles_dry_call/fulfills_stubbing.rb,
lib/mocktail/sorbet/mocktail/registers_stubbing.rb,
lib/mocktail/value/unsatisfying_call_explanation.rb,
lib/mocktail/handles_dry_call/validates_arguments.rb,
lib/mocktail/sorbet/mocktail/handles_dry_new_call.rb,
lib/mocktail/sorbet/mocktail/initializes_mocktail.rb,
lib/mocktail/sorbet/mocktail/matcher_presentation.rb,
lib/mocktail/verifies_call/finds_verifiable_calls.rb,
lib/mocktail/sorbet/mocktail/records_demonstration.rb,
lib/mocktail/sorbet/mocktail/share/cleans_backtrace.rb,
lib/mocktail/sorbet/mocktail/share/stringifies_call.rb,
lib/mocktail/sorbet/mocktail/value/explanation_data.rb,
lib/mocktail/sorbet/mocktail/value/fake_method_data.rb,
lib/mocktail/sorbet/mocktail/value/matcher_registry.rb,
lib/mocktail/sorbet/mocktail/value/type_replacement.rb,
lib/mocktail/imitates_type/ensures_imitation_support.rb,
lib/mocktail/sorbet/mocktail/value/unsatisfying_call.rb,
lib/mocktail/verifies_call/raises_verification_error.rb,
lib/mocktail/sorbet/mocktail/share/creates_identifier.rb,
lib/mocktail/sorbet/mocktail/simulates_argument_error.rb,
lib/mocktail/replaces_type/redefines_singleton_methods.rb,
lib/mocktail/sorbet/mocktail/value/no_explanation_data.rb,
lib/mocktail/simulates_argument_error/recreates_message.rb,
lib/mocktail/simulates_argument_error/transforms_params.rb,
lib/mocktail/sorbet/mocktail/handles_dry_call/logs_call.rb,
lib/mocktail/sorbet/mocktail/imitates_type/makes_double.rb,
lib/mocktail/sorbet/mocktail/replaces_type/redefines_new.rb,
lib/mocktail/sorbet/mocktail/value/type_replacement_data.rb,
lib/mocktail/sorbet/mocktail/raises_neato_no_method_error.rb,
lib/mocktail/sorbet/mocktail/stringifies_method_signature.rb,
lib/mocktail/imitates_type/makes_double/declares_dry_class.rb,
lib/mocktail/sorbet/mocktail/share/stringifies_method_name.rb,
lib/mocktail/sorbet/mocktail/share/determines_matching_calls.rb,
lib/mocktail/sorbet/mocktail/grabs_original_method_parameters.rb,
lib/mocktail/sorbet/mocktail/handles_dry_call/fulfills_stubbing.rb,
lib/mocktail/sorbet/mocktail/value/unsatisfying_call_explanation.rb,
lib/mocktail/simulates_argument_error/reconciles_args_with_params.rb,
lib/mocktail/sorbet/mocktail/handles_dry_call/validates_arguments.rb,
lib/mocktail/sorbet/mocktail/verifies_call/finds_verifiable_calls.rb,
lib/mocktail/handles_dry_call/fulfills_stubbing/finds_satisfaction.rb,
lib/mocktail/replaces_type/runs_sorbet_sig_blocks_before_replacement.rb,
lib/mocktail/sorbet/mocktail/imitates_type/ensures_imitation_support.rb,
lib/mocktail/sorbet/mocktail/verifies_call/raises_verification_error.rb,
lib/mocktail/sorbet/mocktail/replaces_type/redefines_singleton_methods.rb,
lib/mocktail/sorbet/mocktail/simulates_argument_error/recreates_message.rb,
lib/mocktail/sorbet/mocktail/simulates_argument_error/transforms_params.rb,
lib/mocktail/imitates_type/makes_double/gathers_fakeable_instance_methods.rb,
lib/mocktail/sorbet/mocktail/imitates_type/makes_double/declares_dry_class.rb,
lib/mocktail/imitates_type/makes_double/declares_dry_class/reconstructs_call.rb,
lib/mocktail/verifies_call/raises_verification_error/gathers_calls_of_method.rb,
lib/mocktail/handles_dry_call/fulfills_stubbing/describes_unsatisfied_stubbing.rb,
lib/mocktail/sorbet/mocktail/simulates_argument_error/reconciles_args_with_params.rb,
lib/mocktail/sorbet/mocktail/handles_dry_call/fulfills_stubbing/finds_satisfaction.rb,
lib/mocktail/sorbet/mocktail/replaces_type/runs_sorbet_sig_blocks_before_replacement.rb,
lib/mocktail/sorbet/mocktail/imitates_type/makes_double/gathers_fakeable_instance_methods.rb,
lib/mocktail/sorbet/mocktail/imitates_type/makes_double/declares_dry_class/reconstructs_call.rb,
lib/mocktail/sorbet/mocktail/verifies_call/raises_verification_error/gathers_calls_of_method.rb,
lib/mocktail/sorbet/mocktail/handles_dry_call/fulfills_stubbing/describes_unsatisfied_stubbing.rb

Overview

typed: strict

Defined Under Namespace

Modules: Bind, DSL, Debug, ExplanationData, Matchers Classes: AmbiguousDemonstrationError, Cabinet, Call, CleansBacktrace, CollectsCalls, CreatesIdentifier, DeclaresDryClass, DemoConfig, DescribesUnsatisfiedStubbing, DeterminesMatchingCalls, Double, DoubleData, DoubleExplanation, EnsuresImitationSupport, Error, ExplainsNils, ExplainsThing, Explanation, FakeMethodData, FakeMethodExplanation, FindsSatisfaction, FindsVerifiableCalls, FulfillsStubbing, GathersCallsOfMethod, GathersFakeableInstanceMethods, GrabsOriginalMethodParameters, HandlesDryCall, HandlesDryNewCall, ImitatesType, InitializesMocktail, InvalidMatcherError, LogsCall, MakesDouble, MatcherPresentation, MatcherRegistry, MissingDemonstrationError, NoExplanation, NoExplanationData, Params, RaisesNeatoNoMethodError, RaisesVerificationError, ReconcilesArgsWithParams, ReconstructsCall, RecordsDemonstration, RecreatesMessage, RedefinesNew, RedefinesSingletonMethods, RegistersMatcher, RegistersStubbing, ReplacedTypeExplanation, ReplacesNext, ReplacesType, ResetsState, RunsSorbetSigBlocksBeforeReplacement, Signature, SimulatesArgumentError, StringifiesCall, StringifiesMethodName, StringifiesMethodSignature, Stubbing, TopShelf, TransformsParams, TypeCheckingError, TypeReplacement, TypeReplacementData, UnexpectedError, UnsatisfyingCall, UnsatisfyingCallExplanation, UnsupportedMocktail, ValidatesArguments, VerificationError, VerifiesCall

Constant Summary collapse

BASE_PATH =
T.let((Pathname.new(__FILE__) + "..").to_s, String)
TYPED =
true
VERSION =
"2.0.0"

Class Method Summary collapse

Methods included from DSL

stubs, verify

Class Method Details

.cabinetObject

Stores most transactional state about calls & stubbing configurations Anything returned by this is undocumented and could change at any time, so don’t commit code that relies on it!



123
124
125
# File 'lib/mocktail.rb', line 123

def self.cabinet
  Thread.current[:mocktail_store] ||= Cabinet.new
end

.calls(double, method_name = nil) ⇒ Object

An alias for Mocktail.explain(double).reference.calls Takes an optional second parameter of the method name to filter only calls to that method



115
116
117
# File 'lib/mocktail.rb', line 115

def self.calls(double, method_name = nil)
  CollectsCalls.new.collect(double, method_name&.to_sym)
end

.captorObject



83
84
85
# File 'lib/mocktail.rb', line 83

def self.captor
  Matchers::Captor.new
end

.explain(thing) ⇒ Object



103
104
105
# File 'lib/mocktail.rb', line 103

def self.explain(thing)
  ExplainsThing.new.explain(thing)
end

.explain_nilsObject



107
108
109
# File 'lib/mocktail.rb', line 107

def self.explain_nils
  ExplainsNils.new.explain
end

.matchersObject



79
80
81
# File 'lib/mocktail.rb', line 79

def self.matchers
  MatcherPresentation.new
end

.of(type) ⇒ Object

Returns an instance of ‘type` whose implementation is mocked out



46
47
48
# File 'lib/mocktail.rb', line 46

def self.of(type)
  ImitatesType.new.imitate(type)
end

.of_next(type, count: 1) ⇒ Object

Returns an instance of ‘klass` whose implementation is mocked out AND stubs its constructor to return that fake the next time klass.new is called



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mocktail.rb', line 53

def self.of_next(type, count: 1)
  count ||= 1
  if count == 1
    ReplacesNext.new.replace_once(type)
  elsif !Mocktail::TYPED || T::Private::RuntimeLevels.default_checked_level == :never
    ReplacesNext.new.replace(type, count)
  else
    raise TypeCheckingError.new <<~MSG
      Calling `Mocktail.of_next()' with a `count' value other than 1 is not supported when
      type checking is enabled. There are two ways to fix this:

      1. Use `Mocktail.of_next_with_count(type, count)' instead, which will always return
         an array of fake objects.

      2. Disable runtime type checking by setting `T::Private::RuntimeLevels.default_checked_level = :never'
         or by setting the envronment variable `SORBET_RUNTIME_DEFAULT_CHECKED_LEVEL=never'
    MSG
  end
end

.of_next_with_count(type, count) ⇒ Object

An alias of of_next that always returns an array of fakes



75
76
77
# File 'lib/mocktail.rb', line 75

def self.of_next_with_count(type, count)
  ReplacesNext.new.replace(type, count)
end

.register_matcher(matcher) ⇒ Object



87
88
89
# File 'lib/mocktail.rb', line 87

def self.register_matcher(matcher)
  RegistersMatcher.new.register(matcher)
end

.replace(type) ⇒ Object

Replaces every singleton method on ‘type` with a fake, and when instantiated or included will also fake instance methods



94
95
96
97
# File 'lib/mocktail.rb', line 94

def self.replace(type)
  ReplacesType.new.replace(type)
  nil
end

.resetObject



99
100
101
# File 'lib/mocktail.rb', line 99

def self.reset
  ResetsState.new.reset
end