Class: GenSpec::Matchers::GenerationMethodMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/genspec/matchers/generation_method_matcher.rb

Constant Summary collapse

GENERATION_CLASSES =

The modules whose public instance methods will be converted into GenSpec matchers. See #generation_methods for details.

By default, this includes all of the following:

  • Thor::Actions

  • Rails::Generators::Actions

  • Rails::Generators::Migration

If Rails has not been loaded, (e.g. you are testing Thor generators, not Rails generators), the Rails modules are silently ignored.

You can add any additional modules to this list. Note that you should list them in the form of a String representing the module name, rather than adding the modules themselves. This allows you to add them prior to actually load them.

This will only take effect before the specs have been executed; it is best done from within the spec_helper.rb file during the load process.

[ 'Thor::Actions', 'Rails::Generators::Actions', 'Rails::Generators::Migration' ]

Instance Attribute Summary collapse

Attributes inherited from Base

#args, #block, #destination_root, #error, #generator, #init_blocks

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#match!, #matched?, #matches?, #source_root

Constructor Details

#initialize(method_name, *args, &block) ⇒ GenerationMethodMatcher

Returns a new instance of GenerationMethodMatcher.



25
26
27
28
29
30
# File 'lib/genspec/matchers/generation_method_matcher.rb', line 25

def initialize(method_name, *args, &block)
  @method_name = method_name
  @method_args = args
  @actual_args = nil
  super(&block)
end

Instance Attribute Details

#method_argsObject (readonly)

Returns the value of attribute method_args.



23
24
25
# File 'lib/genspec/matchers/generation_method_matcher.rb', line 23

def method_args
  @method_args
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



23
24
25
# File 'lib/genspec/matchers/generation_method_matcher.rb', line 23

def method_name
  @method_name
end

Class Method Details

.for_method(which, *args, &block) ⇒ Object

called from GenSpec::Matchers#call_action

example:

subject.should call_action(:create_file, ...)

equivalent to:

subject.should GenSpec::Matchers::GenerationMethodMatcher.for_method(:create_file, ...)


135
136
137
138
139
140
141
142
143
# File 'lib/genspec/matchers/generation_method_matcher.rb', line 135

def for_method(which, *args, &block)
  if generation_methods.include?(which.to_s)
    new(which, *args, &block)
  else
    raise "Could not find a matcher for '#{which.inspect}'!\n\n" \
          "If this is a custom action, try adding the Thor Action module to GenSpec:\n\n" \
          "  GenSpec::Matchers::GenerationMethodMatcher::GENERATION_CLASSES << 'My::Actions'"
  end
end

.generation_methodsObject

Returns all public instance methods found in the modules listed in GENERATION_CLASSES. This is the list of methods that will be converted into matchers, which can be used like so:

subject.should create_file(. . .)

See also GENERATION_CLASSES



116
117
118
119
120
121
122
123
124
125
# File 'lib/genspec/matchers/generation_method_matcher.rb', line 116

def generation_methods
  GENERATION_CLASSES.inject([]) do |arr, mod|
    if mod.kind_of?(String)
      next arr if !defined?(Rails) && mod =~ /^Rails/
      mod = mod.constantize
    end
    arr.concat mod.public_instance_methods.collect { |i| i.to_s }.reject { |i| i =~ /=/ }
    arr
  end.uniq.sort
end

Instance Method Details

#failure_messageObject



41
42
43
# File 'lib/genspec/matchers/generation_method_matcher.rb', line 41

def failure_message
  "expected to generate a call to #{method_name.inspect}#{with_args} but #{what}"
end

#negative_failure_messageObject



45
46
47
# File 'lib/genspec/matchers/generation_method_matcher.rb', line 45

def negative_failure_message
  "expected not to generate a call to #{method_name.inspect}#{with_args} but it happened anyway"
end

#report_actual_args(args) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/genspec/matchers/generation_method_matcher.rb', line 32

def report_actual_args(args)
  # save a reference to the set of args that most *closely* matched the expectation.
  return(@actual_args = args) if @actual_args.nil?
  matches = (method_args % args).length
  if matches > (method_args % @actual_args).length
    @actual_args = args
  end
end