diff --git a/lib/shoulda/active_record_helpers.rb b/lib/shoulda/active_record_helpers.rb index 4aa92fc9..6ccb412b 100644 --- a/lib/shoulda/active_record_helpers.rb +++ b/lib/shoulda/active_record_helpers.rb @@ -325,7 +325,33 @@ module ThoughtBot # :nodoc: assert reflection, "#{klass.name} does not have any relationship to #{association}" assert_equal :belongs_to, reflection.macro fk = reflection.options[:foreign_key] || "#{association}_id" - assert klass.column_names.include?(fk), "#{klass.name} does not have a #{fk} foreign key." + assert klass.column_names.include?(fk.to_s), "#{klass.name} does not have a #{fk} foreign key." + end + end + end + + # Ensure that the given class methods are defined on the model. + # + # should_have_class_methods :find, :destroy + def should_have_class_methods(*methods) + get_options!(methods) + klass = model_class + methods.each do |method| + should "respond to class method #{method}" do + assert_respond_to klass, method, "#{klass.name} does not have class method #{method}" + end + end + end + + # Ensure that instance methods exist. + # + # should_have_instance_methods :instance_method + def should_have_instance_methods(*methods) + get_options!(methods) + klass = model_class + 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}" end end end diff --git a/test/rails_root/app/models/post.rb b/test/rails_root/app/models/post.rb index 5b38ae42..5b840b98 100644 --- a/test/rails_root/app/models/post.rb +++ b/test/rails_root/app/models/post.rb @@ -1,5 +1,6 @@ class Post < ActiveRecord::Base belongs_to :user + belongs_to :owner, :foreign_key => :user_id, :class_name => 'User' has_many :taggings has_many :tags, :through => :taggings diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index cb9a610e..99051478 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -4,6 +4,7 @@ class PostTest < Test::Unit::TestCase load_all_fixtures should_belong_to :user + should_belong_to :owner should_have_many :tags, :through => :taggings should_require_unique_attributes :title diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 1fc74d89..db011101 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -10,4 +10,6 @@ class UserTest < Test::Unit::TestCase should_ensure_length_in_range :email, 1..100 should_ensure_value_in_range :age, 1..100 should_protect_attributes :password + should_have_class_methods :find, :destroy + should_have_instance_methods :email, :age end