Permalink
Browse files

Renamed model_class to described_type, and made it public

  • Loading branch information...
1 parent 79c7b87 commit e7a0650a396b429a9a4fd28b0b5e379b479049c5 @jferris jferris committed May 6, 2009
@@ -38,7 +38,7 @@ module Macros
#
def should_validate_presence_of(*attributes)
message = get_options!(attributes, :message)
- klass = model_class
+ klass = described_type
attributes.each do |attribute|
matcher = validate_presence_of(attribute).with_message(message)
@@ -71,7 +71,7 @@ def should_validate_uniqueness_of(*attributes)
scope = [*scope].compact
case_sensitive = true if case_sensitive.nil?
- klass = model_class
+ klass = described_type
attributes.each do |attribute|
matcher = validate_uniqueness_of(attribute).
@@ -89,7 +89,7 @@ def should_validate_uniqueness_of(*attributes)
#
def should_allow_mass_assignment_of(*attributes)
get_options!(attributes)
- klass = model_class
+ klass = described_type
attributes.each do |attribute|
matcher = allow_mass_assignment_of(attribute)
@@ -105,7 +105,7 @@ def should_allow_mass_assignment_of(*attributes)
#
def should_not_allow_mass_assignment_of(*attributes)
get_options!(attributes)
- klass = model_class
+ klass = described_type
attributes.each do |attribute|
matcher = allow_mass_assignment_of(attribute)
@@ -121,7 +121,7 @@ def should_not_allow_mass_assignment_of(*attributes)
#
def should_have_readonly_attributes(*attributes)
get_options!(attributes)
- klass = model_class
+ klass = described_type
attributes.each do |attribute|
matcher = have_readonly_attribute(attribute)
@@ -146,7 +146,7 @@ def should_have_readonly_attributes(*attributes)
#
def should_not_allow_values_for(attribute, *bad_values)
message = get_options!(bad_values, :message)
- klass = model_class
+ klass = described_type
bad_values.each do |value|
matcher = allow_value(value).for(attribute).with_message(message)
should "not #{matcher.description}" do
@@ -166,7 +166,7 @@ def should_not_allow_values_for(attribute, *bad_values)
#
def should_allow_values_for(attribute, *good_values)
get_options!(good_values)
- klass = model_class
+ klass = described_type
good_values.each do |value|
matcher = allow_value(value).for(attribute)
should matcher.description do
@@ -194,7 +194,7 @@ def should_ensure_length_in_range(attribute, range, opts = {})
short_message, long_message = get_options!([opts],
:short_message,
:long_message)
- klass = model_class
+ klass = described_type
matcher = ensure_length_of(attribute).
is_at_least(range.first).
@@ -222,7 +222,7 @@ def should_ensure_length_in_range(attribute, range, opts = {})
#
def should_ensure_length_at_least(attribute, min_length, opts = {})
short_message = get_options!([opts], :short_message)
- klass = model_class
+ klass = described_type
matcher = ensure_length_of(attribute).
is_at_least(min_length).
@@ -248,7 +248,7 @@ def should_ensure_length_at_least(attribute, min_length, opts = {})
#
def should_ensure_length_is(attribute, length, opts = {})
message = get_options!([opts], :message)
- klass = model_class
+ klass = described_type
matcher = ensure_length_of(attribute).
is_equal_to(length).
with_message(message)
@@ -278,7 +278,7 @@ def should_ensure_value_in_range(attribute, range, opts = {})
:message,
:low_message,
:high_message)
- klass = model_class
+ klass = described_type
matcher = ensure_inclusion_of(attribute).
in_range(range).
with_message(message).
@@ -304,7 +304,7 @@ def should_ensure_value_in_range(attribute, range, opts = {})
#
def should_validate_numericality_of(*attributes)
message = get_options!(attributes, :message)
- klass = model_class
+ klass = described_type
attributes.each do |attribute|
matcher = validate_numericality_of(attribute).
with_message(message)
@@ -329,7 +329,7 @@ def should_validate_numericality_of(*attributes)
#
def should_have_many(*associations)
through, dependent = get_options!(associations, :through, :dependent)
- klass = model_class
+ klass = described_type
associations.each do |association|
matcher = have_many(association).through(through).dependent(dependent)
should matcher.description do
@@ -350,7 +350,7 @@ def should_have_many(*associations)
#
def should_have_one(*associations)
dependent, through = get_options!(associations, :dependent, :through)
- klass = model_class
+ klass = described_type
associations.each do |association|
matcher = have_one(association).dependent(dependent).through(through)
should matcher.description do
@@ -366,7 +366,7 @@ def should_have_one(*associations)
#
def should_have_and_belong_to_many(*associations)
get_options!(associations)
- klass = model_class
+ klass = described_type
associations.each do |association|
matcher = have_and_belong_to_many(association)
@@ -382,7 +382,7 @@ def should_have_and_belong_to_many(*associations)
#
def should_belong_to(*associations)
dependent = get_options!(associations, :dependent)
- klass = model_class
+ klass = described_type
associations.each do |association|
matcher = belong_to(association).dependent(dependent)
should matcher.description do
@@ -397,7 +397,7 @@ def should_belong_to(*associations)
#
def should_have_class_methods(*methods)
get_options!(methods)
- klass = model_class
+ klass = described_type
methods.each do |method|
should "respond to class method ##{method}" do
assert_respond_to klass, method, "#{klass.name} does not have class method #{method}"
@@ -411,7 +411,7 @@ def should_have_class_methods(*methods)
#
def should_have_instance_methods(*methods)
get_options!(methods)
- klass = model_class
+ klass = described_type
methods.each do |method|
should "respond to instance method ##{method}" do
assert_respond_to klass.new, method, "#{klass.name} does not have instance method #{method}"
@@ -436,7 +436,7 @@ def should_have_db_columns(*columns)
column_type, precision, limit, default, null, scale, sql_type =
get_options!(columns, :type, :precision, :limit,
:default, :null, :scale, :sql_type)
- klass = model_class
+ klass = described_type
columns.each do |name|
matcher = have_db_column(name).
of_type(column_type).
@@ -469,7 +469,7 @@ def should_have_db_columns(*columns)
#
def should_have_db_indices(*columns)
unique = get_options!(columns, :unique)
- klass = model_class
+ klass = described_type
columns.each do |column|
matcher = have_db_index(column).unique(unique)
@@ -510,7 +510,7 @@ def should_have_indices(*args)
#
def should_validate_acceptance_of(*attributes)
message = get_options!(attributes, :message)
- klass = model_class
+ klass = described_type
attributes.each do |attribute|
matcher = validate_acceptance_of(attribute).with_message(message)
@@ -556,7 +556,7 @@ def should_validate_acceptance_of(*attributes)
# end
#
def should_have_named_scope(scope_call, find_options = nil)
- klass = model_class
+ klass = described_type
matcher = have_named_scope(scope_call).finding(find_options)
should matcher.description do
assert_accepts matcher.in_context(self), klass.new
View
@@ -170,6 +170,14 @@ def context(name, &blk)
context.build
end
end
+
+ # Returns the class being tested, as determined by the test class name.
+ #
+ # class UserTest; described_type; end
+ # # => User
+ def described_type
+ self.name.gsub(/Test$/, '').constantize
+ end
end
class Context # :nodoc:
@@ -9,12 +9,5 @@ def get_options!(args, *wanted)
raise ArgumentError, "Unsupported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
return *ret
end
-
- # Returns the model class constant, as determined by the test class name.
- #
- # class TestUser; model_class; end => User
- def model_class
- self.name.gsub(/Test$/, '').constantize
- end
end
end
@@ -142,4 +142,12 @@ def hello; "hi"; end
should_eventually "only print this statement once for a should_eventually"
end
end
+
+ class ::SomeModel; end
+ context "described_type" do
+ should "sniff the class constant from the test class" do
+ self.class.expects(:name).returns("SomeModelTest")
+ assert_equal SomeModel, self.class.described_type
+ end
+ end
end
@@ -23,12 +23,4 @@ class PrivateHelpersTest < ActiveSupport::TestCase # :nodoc:
end
end
end
-
- class ::SomeModel; end
- context "model_class" do
- should "sniff the class constant from the test class" do
- self.expects(:name).returns("SomeModelTest")
- assert_equal SomeModel, model_class
- end
- end
end

1 comment on commit e7a0650

Contributor

hardbap commented on e7a0650 May 7, 2009

This is one of those little things that makes a man happy. Thanks Joe.

Please sign in to comment.