【问题标题】:Check if a method is an alias to another method in rspec检查一个方法是否是 rspec 中另一个方法的别名
【发布时间】:2016-01-21 16:39:38
【问题描述】:

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​​ alias_method,但没有用。

【问题讨论】:

  • 不,它没有。它也在文章实例上定义,而不是作为类方法

标签: ruby methods


【解决方案1】:

Method#== 的定义不明确和/或有用,因此您不应依赖它。

要检查它是否是别名,您可以这样做:

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

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 2020-08-29
    • 2013-08-02
    • 1970-01-01
    • 2021-12-06
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多