提交 5339f813 编写于 作者: P Pratik Naik

Change Object#try to raise NoMethodError on private methods and always return...

Change Object#try to raise NoMethodError on private methods and always return nil when Object is nil [Pratik Naik, Lawrence Pit]
上级 296ca4da
......@@ -484,7 +484,8 @@ def method_missing(selector, *args, &block)
#
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
def fixture_file_upload(path, mime_type = nil, binary = false)
ActionController::TestUploadedFile.new("#{ActionController::TestCase.try(:fixture_path)}#{path}", mime_type, binary)
fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary)
end
# A helper to make it easier to test different route configurations.
......
......@@ -102,6 +102,6 @@ def acts_like?(duck)
# Person.try(:find, 1)
# @people.try(:map) {|p| p.name}
def try(method, *args, &block)
send(method, *args, &block) if respond_to?(method, true)
send(method, *args, &block) unless self.nil?
end
end
......@@ -256,21 +256,13 @@ def setup
def test_nonexisting_method
method = :undefined_method
assert !@string.respond_to?(method)
assert_nil @string.try(method)
assert_raises(NoMethodError) { @string.try(method) }
end
def test_valid_method
assert_equal 5, @string.try(:size)
end
def test_valid_private_method
class << @string
private :size
end
assert_equal 5, @string.try(:size)
end
def test_argument_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
end
......@@ -278,4 +270,13 @@ def test_argument_forwarding
def test_block_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
end
def test_nil_to_type
assert_nil nil.try(:to_s)
assert_nil nil.try(:to_i)
end
def test_false_try
assert_equal 'false', false.try(:to_s)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册