You are now browsing Ruby on Rails category.

August 9th, 2011 11:10   |   Category: Ruby on Rails

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: