检查方法是否是rspec中另一个方法的别名

时间:2016-01-21 16:39:39

标签: ruby methods

Article#to_archiveArticle#archived!的别名:

class Article
  alias to_archive archived!
end

我需要确保这一点,所以我写了这个测试:

describe '#to_archive' do
  it 'is an alias to #archived!' do
    expect(subject.method(:to_archive)).to eq(subject.method(:archived!))
  end
end

但是,我收到错误

Failure/Error: expect(subject.method(:to_archive)).to eq(subject.method(:archived!))

   expected: #<Method: Article(#<Module:0x00000005a7c240>)#archived!>
        got: #<Method: Article(#<Module:0x00000005a7c240>)#to_archive(archived!)>

它曾用于ruby&lt; 2.3 IIRC。我试过了alias_method,但没有用。

1 个答案:

答案 0 :(得分:5)

Method#==的定义不明确和/或有用,所以你不应该依赖它。

要检查它是否为别名,您可以执行以下操作:

expect(subject.method(:to_archive).original_name).to eq(:archived!)