RSpec Stubbing Java静态方法ArgumentError

时间:2018-01-16 20:28:22

标签: ruby-on-rails rspec jruby jrubyonrails jco

我试图在我的规范中为我的JRuby类存储Java静态方法,该类导入了一些Java库。我为方法调用收到以下错误:

Failure/Error: JCoDestinationManager.getDestination('properties')          
ArgumentError:                                               
  Wrong number of arguments. Expected 0, got 1.

这是JCo Java静态方法。我创建了一个小的spec文件来尝试找出问题:

require 'rails_helper'                                                                                                      

describe SapClient do                                                                                                              
  let(:destination) { double(Java::ComSapConnJcoRt::RfcDestination) }                                                              

  before do                                                                                                                        
    allow(JCoDestinationManager).to receive(:getDestination).and_return destination                                                
  end                                                                                                                              

  it 'can be created' do                                                                                                           
    c = SapClient.new                                                                                                              
    expect(c).to_not be_nil                                                                                                        
  end                                                                                                                              
end

如果我像这样添加with子句,我仍会得到相同的结果。

allow(JCoDestinationManager).to receive(:getDestination).with('properties').and_return destination

对我来说,奇怪的是我只在rails中遇到这个项目。如果我将代码复制到Sinatra项目并在那里运行这些规范,一切都很好。我创建了一个新的Rails项目和一个新的Sinatra项目并安装了相同版本的RSpec,经过验证的RSpec模拟版本是相同的,等等仍然看到了行为上的差异。

当我撬开并观察当我打电话时发生的事情

JCoDestinationManager.getDestination

没有参数,我得到了我定义的模拟目的地。如果我注释掉allow语句,我会看到一个真正的RfcDestination被创建。

在搜索时,我看到的东西似乎与我在这里观察的内容差不多来自https://github.com/rspec/rspec-mocks/issues/919,但显然这个问题的解决方案是一个已经解决的问题。

Rails Gemfile

Rails Gemfile.lock

Sinatra Gemfile

Sinatra Gemfile.lock

0 个答案:

没有答案
相关问题