Class: Kicker::Recipes::Rails

Inherits:
Ruby
  • Object
show all
Defined in:
lib/kicker/recipes/rails.rb

Instance Attribute Summary

Attributes inherited from Ruby

#tests

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ruby

call, #initialize, reset!, run_tests, run_with_spec_runner, run_with_test_runner, #runner_bin, runner_command, spec_runner_command, #test_cases_root, #test_file, test_runner_command, #test_type

Constructor Details

This class inherits a constructor from Kicker::Recipes::Ruby

Class Method Details

.all_controller_testsObject

Returns an array consiting of all controller tests.



38
39
40
41
42
43
44
# File 'lib/kicker/recipes/rails.rb', line 38

def all_controller_tests
  if test_type == 'test'
    Dir.glob("#{test_cases_root}/functional/**/*_test.rb")
  else
    Dir.glob("#{test_cases_root}/controllers/**/*_spec.rb")
  end
end

.type_to_test_dir(type) ⇒ Object

Maps type, for instance ‘models’, to a test directory.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kicker/recipes/rails.rb', line 11

def type_to_test_dir(type)
  if test_type == 'test'
    case type
    when "models"
      "unit"
    when "concerns"
      "unit/concerns"
    when "controllers", "views"
      "functional"
    when "helpers"
      "unit/helpers"
    end
  elsif test_type == 'spec'
    case type
    when "models"
      "models"
    when "concerns"
      "models/concerns"
    when "controllers", "views"
      "controllers"
    when "helpers"
      "helpers"
    end
  end
end

Instance Method Details

#handle!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kicker/recipes/rails.rb', line 64

def handle!
  @tests.concat(@files.take_and_map do |file|
    case file
    # Run all functional tests when routes.rb is saved
    when 'config/routes.rb'
      Kicker::Recipes::Rails.all_controller_tests

    # Match lib/*
    when /^(lib\/.+)\.rb$/
      test_file($1)

    # Map fixtures to their related tests
    when %r{^#{test_cases_root}/fixtures/(\w+)\.yml$}
      tests_for_model($1)

    # Match any file in app/ and map it to a test file
    when %r{^app/(\w+)([\w/]*)/([\w\.]+)\.\w+$}
      type, namespace, file = $1, $2, $3

      if dir = Kicker::Recipes::Rails.type_to_test_dir(type)
        if type == "views"
          namespace = namespace.split('/')[1..-1]
          file = "#{namespace.pop}_controller"
        end

        test_file File.join(dir, namespace, file)
      end
    end
  end)

  # And let the Ruby handler match other stuff.
  super
end

#tests_for_model(model) ⇒ Object

Returns an array of all tests related to the given model.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kicker/recipes/rails.rb', line 48

def tests_for_model(model)
  if test_type == 'test'
    %W{
      unit/#{ActiveSupport::Inflector.singularize(model)}
      unit/helpers/#{ActiveSupport::Inflector.pluralize(model)}_helper
      functional/#{ActiveSupport::Inflector.pluralize(model)}_controller
    }
  else
    %W{
      models/#{ActiveSupport::Inflector.singularize(model)}
      helpers/#{ActiveSupport::Inflector.pluralize(model)}_helper
      controllers/#{ActiveSupport::Inflector.pluralize(model)}_controller
    }
  end.map { |f| test_file f }
end