【问题标题】:org.jruby.exceptions.RaiseException: (NameError) uninitialized constantorg.jruby.exceptions.RaiseException: (NameError) 未初始化的常量
【发布时间】:2018-06-26 09:56:50
【问题描述】:

我从 ActiveMerchant 库的 JRuby 包装中收到以下错误:

org.jruby.exceptions.RaiseException: (NameError) uninitialized constant ActiveMerchant::Billing::SecurePayAuGateway
    at org.jruby.RubyModule.const_missing(org/jruby/RubyModule.java:3345)
    at org.jruby.RubyModule.const_get(org/jruby/RubyModule.java:3290)
    at RUBY.createGateway(classpath:/scripts/main.rb:79)

启动它的代码是:

gateway = ActiveMerchant::Billing::const_get(name).new(options)

我认为这是因为 gateways.rb 中发生了一些网关的动态加载:

module ActiveMerchant
    module Billing
        load_path = Pathname.new(__FILE__ + '/../../..')
        Dir[File.dirname(__FILE__) + '/gateways/**/*.rb'].each do |filename|
            gateway_name      = File.basename(filename, '.rb')
            gateway_classname = "#{gateway_name}_gateway".camelize
            gateway_filename  = Pathname.new(filename).relative_path_from(load_path).sub_ext('')

            autoload(gateway_classname, gateway_filename)
        end
    end
end

这在单元测试期间有效,因为 Ruby 文件是我的目标目录中的真实文件。但是在最终的应用程序中,Ruby 文件包含在 Jar 中,而 Jar 位于 Jar 中。

有人知道为什么会发生这种情况以及如何让它发挥作用吗?

【问题讨论】:

    标签: ruby jruby activemerchant


    【解决方案1】:

    你想教哪个网关?来自 Active Merchant 的 github 没有

    ActiveMerchant::Billing::SecurePayAuGateway
    

    但是有一个

    ActiveMerchant::Billing::SecurePayGateway
    

    还有一个

    ActiveMerchant::Billing::SecurePayTechGateway
    

    测试,而不是

    gateway = ActiveMerchant::Billing::const_get(name).new(options)
    

    试试

    gateway = ActiveMerchant::Billing::SecurePayGateway
    

    如果可行,您就知道问题出在您的 gateway = 方法上,您可以相应地解决此问题

    【讨论】:

    • SecurePayAuGateway 在这里:github.com/activemerchant/active_merchant/blob/master/lib/…。我已经调试了 JRuby,可以看到当 gem 被包装到 jar 中时,没有一个网关被加载为 Billing 模块的常量,但是它们都在单元测试期间被加载,因为单元测试引用了 Maven 目标中的真实文件目录。
    猜你喜欢
    • 1970-01-01
    • 2015-01-19
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2016-06-12
    • 2015-03-01
    相关资源
    最近更新 更多