0

我有一个运行 ssh 会话的 ruby​​ 脚本(scratch/ssh.rb):

require 'net/ssh'
require 'net/ssh/shell'

def try_process
    Net::SSH.start('host', 'username', {:password=>'pwd'}) do |ssh|
    end
end

我写了一个规范(spec/test_ssh.rb)如下:

require 'scratch/ssh'

describe 'SSH Tester' do
  it "should work" do
    try_process
  end
end

我尝试按如下方式运行规范:

bundle exec rspec -I. spec/test_ssh.rb

并得到以下结果:

Failures:

  1) SSH Tester should work
     Failure/Error: try_process
     NoMethodError:
       undefined method `timeout' for #<Net::SSH::Transport::Session:0x000000036d5288>
     # ./scratch/ssh.rb:6:in `try_process'
     # ./spec/test_ssh.rb:5:in `block (2 levels) in <top (required)>'

有趣的是,运行 ssh.rb 本身会成功,所以我认为它与 RSpec 有关。

我试图查找 net-ssh 源代码,发现这里确实使用了“超时”,这似乎是一个合法的 Ruby 模块。

我怀疑这可能是由于 rspec 使用不同/剪切版本的 Ruby 解释器来运行测试,但我不知道如何检查它。我试图put LOAD_PATH在规范中,但它没有显示任何可疑之处。

Ruby 版本:1.9.3 Rspec 版本:2.14.7 net-ssh 版本:2.1.4

有谁知道我需要在哪里挖掘才能解决这个问题?

非常感谢!

4

1 回答 1

0

对于它的价值,我试图重现您的问题,但无法重现。

具体来说,如下代码:

require 'net/ssh'
require 'net/ssh/shell'

def try_process
    Net::SSH.start('123.123.123.123', {}) do |ssh|
    end
end

describe "" do
  it "" do
    try_process
  end
end

产生以下输出:

F

Failures:

  1)  
     Failure/Error: Net::SSH.start('123.123.123.123', {}) do |ssh|
     Errno::ETIMEDOUT:
       Operation timed out - connect(2)
     # ./tmp/so_spec.rb:5:in `try_process'
     # ./tmp/so_spec.rb:11:in `block (2 levels) in <top (required)>'

Finished in 1 minute 15.69 seconds
1 example, 1 failure

Failed examples:

rspec ./tmp/so_spec.rb:10 #

表明该timeout方法在 RSpec 块中仍然可以访问。

此测试使用 Ruby 2.0.0p247 和 RSpec 2.14.5 完成

于 2013-12-19T18:03:07.643 回答