Forum: Ruby on Rails Mock Objects (Development)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D0529fc90e3cbf86e57078e8960ebcc9?d=identicon&s=25 Chris Evans (Guest)
on 2006-01-04 21:33
I'm having a problem using mock objects in development (not testing).

I have a class called User. In order to log in, User has to connect to
an external IMAP server and authenticate. I don't want to do this during
development. So, I created a mock User in tests/mock/development that
went something like this:


require 'models/user'

class User
  def login_to_external_server
    :success
  end
end


In this case, it should override User's method
'login_to_external_server', but maintain the rest of its functionality.
This is not the case. When I try to use it in my application, it
pretends like it knows nothing about the real user, and
login_to_external_server is the only function it knows. Am I doing
something wrong?
D0529fc90e3cbf86e57078e8960ebcc9?d=identicon&s=25 Chris Evans (Guest)
on 2006-01-04 21:43
> require 'models/user'

I changed this line to require_dependency 'models/user' and now it
works.
Fcd3b65b9d8d37d7cd91f3159b851b11?d=identicon&s=25 =?UTF-8?B?TWlndWVsIEVucmlxdWUgQ29iw6EgTWFydMOtbmV6 (Guest)
on 2006-01-04 21:48
(Received via mailing list)
Chris Evans wrote:
> class User
> login_to_external_server is the only function it knows. Am I doing
> something wrong?
>
Yeah man, indeed a mock class hides your original class. In the mock
class you should define ALL the methods needed for your app to work. Not
only the method that you think is the one that will have external
effects.
This topic is locked and can not be replied to.