11

在单元测试中,我需要测试 alias_method 定义的别名方法是否已正确定义。我可以简单地对用于其原件的别名使用相同的测试,但我想知道是否有更明确或更有效的解决方案。例如,有没有办法 1) 取消引用方法别名并返回其原始名称,2) 获取并比较某种底层方法标识符或地址,或 3) 获取并比较方法定义?例如:

class MyClass
  def foo
    # do something
  end

  alias_method :bar, :foo
end

describe MyClass do
  it "method bar should be an alias for method foo" do
    m = MyClass.new
    # ??? identity(m.bar).should == identity(m.foo) ???
  end
end

建议?

4

3 回答 3

18

根据Method的文档,

如果两个方法对象绑定到同一个对象并包含相同的主体,则它们是相等的。

调用Object#method并比较Method它返回的对象将验证方法是否等效:

m.method(:bar) == m.method(:foo)
于 2010-10-06T05:44:10.887 回答
3

bk1e 的方法大部分时间都有效,但我只是碰巧遇到了它不起作用的情况:

class Stream
  class << self
    alias_method :open, :new
  end
end

open = Stream.method(:open)
new = Stream.method(:new)
p open, new                   # => #<Method: Stream.new>, #<Method: Class#new>
p open.receiver, new.receiver # => Stream, Stream
p open == new                 # => false

输出是在 Ruby 1.9 中生成的,不确定是否是错误,因为 Ruby 1.8true为最后一行生成。因此,如果您使用的是 1.9,请小心为继承的类方法(如 Class#new)设置别名,这两个方法绑定到同一个对象(类对象Stream),但 Ruby 1.9 认为它们不等效。

我的解决方法很简单 - 再次为原始方法设置别名并测试两个别名的相等性:

class << Stream; alias_method :alias_test_open, :new; end
open = Stream.method(:open)
alias_test_open = Stream.method(:alias_test_open)
p open, alias_test_open                   # => #<Method: Stream.new>, #<Method: Stream.new>
p open.receiver, alias_test_open.receiver # => Stream, Stream
p open == alias_test_open                 # => true

希望这可以帮助。

更新:

http://bugs.ruby-lang.org/issues/7613

所以Method#==在这种情况下应该返回 false,因为super调用会调用不同的方法;这不是一个错误。

于 2012-12-23T21:21:03.717 回答
1

调用MyClass.instance_method(:foo)将产生UnboundMethod实例,该实例具有eql?方法。

所以答案是:

describe MyClass do
  subject { described_class }

  specify do
    expect(subject.instance_method(:foo)).to be_eql(subject.instance_method(:bar))
  end
end
于 2015-06-12T14:10:05.057 回答