【问题标题】:Rspec net-ssh session throws NoMethodErrorRspec net-ssh 会话抛出 NoMethodError
【发布时间】:2013-12-20 02:03:07
【问题描述】:

我有一个运行 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 源并发现 here 它确实使用了“超时”,这似乎是一个合法的 Ruby 模块。

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

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

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

非常感谢!

【问题讨论】:

  • 当您说“运行 ssh.rb 本身成功”时,您的意思是直接运行它还是运行它然后调用 try_process?
  • 我在irb中试过如下:load 'scratch/ssh.rb'; try_process

标签: ruby rspec net-ssh


【解决方案1】:

无论如何,我试图重现您的问题,但无法重现。

具体如下代码:

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 完成的

【讨论】:

  • 嗯。如果我在 ruby​​-2.0.0 上尝试它确实有效。这似乎是一个 Ruby 版本的问题。谢谢。
猜你喜欢
  • 2014-10-18
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多