This is always a tricky problem. One quick way to solve it could be to force the compiler the load the foo
file by using load
instead of require
:
load '../foo.rb' #note the presence of the file extension
However, it's usually a better idea to give your mock classes different names and use dependency injection to indicate which class your object under test should interact with.
class MockFoo
#...
end
class ObjectUnderTest
def method_that_depends_on_foo(foo_class = Foo)
foo_class.do_something
# ...
end
end
In your test, you would call the method using method_that_depends_on_foo(MockFoo)
. Depending on what your method actually does, there might be a better way to go about this, but the point is to think about how to decouple strict dependencies between your classes. If you want to test them in isolation, they need to be isolatable.