Module: RSpec::Forward::ForwardMethods

Included in:
ForwardToInstance, ForwardToInstanceBuild
Defined in:
lib/rspec/forward/forward_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



108
109
110
# File 'lib/rspec/forward/forward_methods.rb', line 108

def self.included(base)
  private :exp_args, :instance, :assign_actual, :matches_for?
end

Instance Method Details

#assign_actual(actual) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/rspec/forward/forward_methods.rb', line 69

def assign_actual(actual)
  @actual ||=
    if actual.is_a?(Class)
      actual
    else
      actual.class
    end
end

#exp_argsObject



57
58
59
60
61
62
63
# File 'lib/rspec/forward/forward_methods.rb', line 57

def exp_args
  if @args.size.zero? && @kwargs.size.zero?
    [no_args]
  else
    @args
  end
end

#failure_messageObject



49
50
51
# File 'lib/rspec/forward/forward_methods.rb', line 49

def failure_message
  "expected #{@target.inspect} to be #{@expected}"
end

#failure_message_when_negatedObject



53
54
55
# File 'lib/rspec/forward/forward_methods.rb', line 53

def failure_message_when_negated
  "expected #{@target.inspect} not to be #{@expected}"
end

#instanceObject



65
66
67
# File 'lib/rspec/forward/forward_methods.rb', line 65

def instance
  @_instance ||= instance_double(@actual, @expected => :return)
end

#matches_for?(actual, return_value) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rspec/forward/forward_methods.rb', line 78

def matches_for?(actual, return_value)
  assign_actual(actual)

  if @kwargs.any?
    allow(@actual)
      .to receive(:new)
      .with(*exp_args, **@kwargs)
      .and_return(instance)

    result = @actual.send(@expected, *@args, **@kwargs) == return_value

    expect(@actual)
      .to have_received(:new)
      .with(*exp_args, **@kwargs)
  else
    allow(@actual)
      .to receive(:new)
      .with(*exp_args)
      .and_return(instance)

    result = @actual.send(@expected, *@args) == return_value

    expect(@actual)
      .to have_received(:new)
      .with(*exp_args)
  end

  result
end

#with(*args, **kwargs) ⇒ Object



37
38
39
40
41
# File 'lib/rspec/forward/forward_methods.rb', line 37

def with(*args, **kwargs)
  @args = args
  @kwargs = kwargs
  self
end

#with_1_argObject



21
22
23
# File 'lib/rspec/forward/forward_methods.rb', line 21

def with_1_arg
  with_1_args
end

#with_1_arg_and_named(*args, **kwargs) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rspec/forward/forward_methods.rb', line 25

def with_1_arg_and_named(*args, **kwargs)
  if kwargs.empty?
    with_1_args_and_named(*args)
  else
    with_1_args_and_named(*args, **kwargs)
  end
end

#with_named(*kwargs) ⇒ Object



43
44
45
46
47
# File 'lib/rspec/forward/forward_methods.rb', line 43

def with_named(*kwargs)
  @args = []
  @kwargs = Hash[kwargs.map { |name| [name, name] }]
  self
end

#with_no_argsObject



33
34
35
# File 'lib/rspec/forward/forward_methods.rb', line 33

def with_no_args
  with_0_args
end