mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
adding a test for no method error
This commit is contained in:
parent
fcd8925f23
commit
f8700038af
2 changed files with 57 additions and 11 deletions
|
@ -286,19 +286,13 @@ module ActiveRecord
|
||||||
private
|
private
|
||||||
|
|
||||||
# Forwards any missing method call to the \target.
|
# Forwards any missing method call to the \target.
|
||||||
def method_missing(method, *args)
|
def method_missing(method, *args, &block)
|
||||||
if load_target
|
if load_target
|
||||||
unless @target.respond_to?(method)
|
return super unless @target.respond_to?(method)
|
||||||
message = "undefined method `#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}"
|
@target.send(method, *args, &block)
|
||||||
raise NoMethodError, message
|
|
||||||
end
|
|
||||||
|
|
||||||
if block_given?
|
|
||||||
@target.send(method, *args) { |*block_args| yield(*block_args) }
|
|
||||||
else
|
|
||||||
@target.send(method, *args)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
rescue NoMethodError => e
|
||||||
|
raise e, e.message.sub(/ for #<.*$/, " via proxy for #{@target}")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Should be true if there is a foreign key present on the @owner which
|
# Should be true if there is a foreign key present on the @owner which
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
require "cases/helper"
|
||||||
|
|
||||||
|
module ActiveRecord
|
||||||
|
module Associations
|
||||||
|
class AsssociationProxyTest < ActiveRecord::TestCase
|
||||||
|
class FakeOwner
|
||||||
|
attr_accessor :new_record
|
||||||
|
alias :new_record? :new_record
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@new_record = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class FakeReflection < Struct.new(:options, :klass)
|
||||||
|
def initialize options = {}, klass = nil
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_validity!
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class FakeTarget
|
||||||
|
end
|
||||||
|
|
||||||
|
class FakeTargetProxy < AssociationProxy
|
||||||
|
def association_scope
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_target
|
||||||
|
FakeTarget.new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_method_missing_error
|
||||||
|
reflection = FakeReflection.new({}, Object.new)
|
||||||
|
owner = FakeOwner.new
|
||||||
|
proxy = FakeTargetProxy.new(owner, reflection)
|
||||||
|
|
||||||
|
exception = assert_raises(NoMethodError) do
|
||||||
|
proxy.omg
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_match('omg', exception.message)
|
||||||
|
assert_match(FakeTarget.name, exception.message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue