Sha256: f40409dd03b352ebdc3407123e0326fb58a45d29bbe548e36bb02b4f44f0df25

Contents?: true

Size: 1.48 KB

Versions: 8

Compression:

Stored size: 1.48 KB

Contents

require "active_support/concern"
require "active_support/inflector"

module Minitest
  module Rails
    # Resolves a constant from a minitest spec name.
    #
    # Given the following spec-style test:
    #
    #   describe WidgetsController, :index do
    #     describe "authenticated user" do
    #       describe "returns widgets" do
    #         it "has a controller that exists" do
    #           assert_kind_of WidgetsController, @controller
    #         end
    #       end
    #     end
    #   end
    #
    # The test will have the following name:
    #
    #   "WidgetsController::index::authenticated user::returns widgets"
    #
    # The constant WidgetsController can be resolved from the name.
    # The following code will resolve the constant:
    #
    #   controller = determine_constant_from_test_name(name) do |constant|
    #     Class === constant && constant < ::ActionController::Metal
    #   end
    module ConstantLookup
      extend ::ActiveSupport::Concern

      module ClassMethods  # :nodoc:
        def determine_constant_from_test_name(test_name)
          names = test_name.split "::"
          while names.size > 0 do
            names.last.sub!(/Test$/, "")
            begin
              constant = names.join("::").constantize
              break(constant) if yield(constant)
            rescue NameError
              # Constant wasn't found, move on
            ensure
              names.pop
            end
          end
        end
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
minitest-rails-3.0.0 lib/minitest/rails/constant_lookup.rb
minitest-rails-3.0.0.rc1 lib/minitest/rails/constant_lookup.rb
minitest-rails-2.2.1 lib/minitest/rails/constant_lookup.rb
minitest-rails-2.2.0 lib/minitest/rails/constant_lookup.rb
minitest-rails-2.1.1 lib/minitest/rails/constant_lookup.rb
minitest-rails-2.1.0 lib/minitest/rails/constant_lookup.rb
minitest-rails-2.0.3 lib/minitest/rails/constant_lookup.rb
minitest-rails-2.0.2 lib/minitest/rails/constant_lookup.rb