Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a method is an alias to another method in rspec

Tags:

methods

ruby

Article#to_archive is an alias for Article#archived!:

class Article
  alias to_archive archived!
end

I need to ensure this, so I wrote this test:

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

However, I receive an error

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!)>

It used to work in ruby < 2.3 IIRC. I tried alias_method, but it didn't help.

like image 872
just so Avatar asked Jan 21 '16 16:01

just so


1 Answers

The definition of Method#== is not clear and/or useful, so you shouldn't rely on it.

To check that it is an alias, you can do this:

expect(subject.method(:to_archive).original_name).to eq(:archived!)
like image 168
sawa Avatar answered Sep 20 '22 15:09

sawa