# How to stub out a library file

**URL:** <https://www.ruby-forum.com/t/how-to-stub-out-a-library-file/182736>\
**Category:** Rails\
**Created:** [February 2, 2010, 3:40am UTC](https://www.ruby-forum.com/t/how-to-stub-out-a-library-file/182736 "2010-02-02T03:40:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jacky](https://www.ruby-forum.com/u/jacky)\
**Post date:** [February 2, 2010, 3:40am UTC](https://www.ruby-forum.com/t/how-to-stub-out-a-library-file/182736/1 "2010-02-02T03:40:12Z")

</div>

Hi all,

I encounter a problem when i’m stubing out a library file.  
The rails version is 2.3.4.

[How to reproduce]

1. Create a file payment\_gateway.rb in subdirectory lib/.

[code]  
class PaymentGateway  
def self.collect  
p ‘lib/payment\_gateway’  
true  
end  
end

1. Create a file payment\_gateway.rb in subdirectory test/mocks/test.

[code]  
require ‘lib/util/payment\_gateway’

class PaymentGateway  
def self.collect  
p ‘mock/payment\_gateway’  
false  
end  
end

1. Create a method in any unit test file to test this method.

The actual result is invoking the method in the mock file, and the  
output is  
‘mock/payment\_gateway’  
false

But if i add the following code in the application\_controller.rb

[code]  
require ‘util/payment\_gateway’

The actual result is invoking the method in the lib file, and the output  
is  
‘lib/payment\_gateway’  
true

I don’t know why is that, could anyone give me some help?

Thanks very much.
