Mock Objects (Development)


#1

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?


#2

require ‘models/user’

I changed this line to require_dependency ‘models/user’ and now it
works.


#3

Chris E. 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.