Özgün Koyun Özgün Koyun

Mocking methods ending with equal sing (=) in (RR) Double Ruby

  ruby testing

So, you are using Double Ruby as mocking framework instead of Rspec’s default mocking framewok, and you want to mock a method which ends with the equal sign(=). Normally, if your method ends with other special characters such as “!” or “?”, you can safely mock those methods as below:

mock(@user).delete_account! { true }
mock(@user).admin? { true }

However, this doesn’t work if your method ends with the “=” sign. Here comes the solution:

mock(@user).__send__('name=').with('Paranoid Android') { }

Now, in your controller, you can expect the following:

@user.name = 'Paranoid Android'

But, why did I use send instead of the simple send ? Take a look at this

Resources