Module: RSpec::Forward::ForwardScopeMethods

Included in:
ForwardToScope
Defined in:
lib/rspec/forward/forward_scope_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



72
73
74
# File 'lib/rspec/forward/forward_scope_methods.rb', line 72

def self.included(base)
  private :matches_for?, :find_actual
end

Instance Method Details

#exp_args(actual) ⇒ Object



6
7
8
9
10
11
# File 'lib/rspec/forward/forward_scope_methods.rb', line 6

def exp_args(actual)
  return [actual, *@args] if @with_parent_arg
  return [no_args] if @args.size.zero? && @kwargs.size.zero?

  @args
end

#failure_messageObject



62
63
64
65
# File 'lib/rspec/forward/forward_scope_methods.rb', line 62

def failure_message
  @failure_message ||
    %Q(expected "#{@target}.call" to be invoked)
end

#failure_message_when_negatedObject



67
68
69
70
# File 'lib/rspec/forward/forward_scope_methods.rb', line 67

def failure_message_when_negated
  @failure_message_when_negated ||
    %Q(expected "#{@target}.call" not to be invoked)
end

#failure_scope_klass_name_not_defined(name) ⇒ Object



57
58
59
60
# File 'lib/rspec/forward/forward_scope_methods.rb', line 57

def failure_scope_klass_name_not_defined(name)
  @failure_message = "#{name} should be defined"
  @failure_message_when_negated = "#{name} should be defined"
end

#find_actual(klass_or_instance) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rspec/forward/forward_scope_methods.rb', line 13

def find_actual(klass_or_instance)
  if klass_or_instance.is_a? Class
    klass_or_instance
  else
    klass_or_instance.class
  end
end

#matches_for?(klass_or_instance) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rspec/forward/forward_scope_methods.rb', line 21

def matches_for?(klass_or_instance)
  actual = find_actual(klass_or_instance)

  method_name = @expected
  base_klass = format("%<method>s_scope", method: method_name.to_s).camelize
  @scope_klass_name ||= format("%s::%s", actual.to_s, base_klass)


  begin
    @scope_klass = Object.const_get(@scope_klass_name)
  rescue NameError => e
    failure_scope_klass_name_not_defined(@scope_klass_name)
    return false
  end

  allow(@scope_klass)
    .to receive(@target_method_name)
    .and_return(:result)

  if @kwargs.any?
    result = klass_or_instance.public_send(method_name, *@args, **@kwargs) == :result

    expect(@scope_klass)
      .to have_received(@target_method_name)
      .with(*exp_args(actual), **@kwargs)
  else
    result = klass_or_instance.public_send(method_name, *@args) == :result

    expect(@scope_klass)
      .to have_received(@target_method_name)
      .with(*exp_args(actual))
  end

  result
end