How to stub out a library file

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs