0

I have a class as follows:

class User
  alias_method :nickname, :username

  def username
    self.name.split(' ').first.downcase
  end
end

I would like to test that the method :nickname is an alias for :username.

Both the following don't work:

user = User.new
User.instance_method(:nick_name) == User.instance_method(:username)
user.method(:nick_name) == user.method(:username)

A similar discussion happened at How to test method alias ruby , but the answers stated there don't work.

Satyaram B V
  • 307
  • 2
  • 10

1 Answers1

1

The answer you shared the link for discusses about class methods, here you have instance methods. and yes, as correctly pointed out by @HoMan in the comments, following should work..

class User
  def username
    self.name.split(' ').first.downcase
  end

  alias nickname, username
end

In you testcases,

user = User.last
user.nick_name == user.username
Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36