【问题标题】:FakeFS causing open-uri open to cause "No such file or directory @ rb_sysopen"FakeFS 导致 open-uri 打开导致“没有这样的文件或目录@rb_sysopen”
【发布时间】:2026-01-03 14:30:01
【问题描述】:

我使用 rails 4.1.4 和 fakefs 0.5.2。 我尝试升级到 rails 4.2.0,但现在我的测试因以下错误而失败:

Errno::ENOENT:
   No such file or directory @ rb_sysopen - https://google.com/

我尝试搜索,最后发现这可能是由 FakeFS 引起的,因为当我在 Rails 控制台中调用 open 时,它按预期工作,如下所示:

2.1.0 :001 > link = open("https://google.com")
 => #<Tempfile:/tmp/open-uri20150227-21959-18cqr0h>
2.1.0 :003 > require 'fakefs/spec_helpers'
 => true 
2.1.0 :004 > FakeFS.activate!
 => true 
2.1.0 :005 > link = open("https://google.com")
 Errno::ENOENT: No such file or directory - https://google.com

有人知道这是什么原因吗?

更新: 所以,我最终决定做的是用curb 替换open-uri。还有其他选择,例如rest-clientNet::HTTP。 谢谢

【问题讨论】:

标签: ruby-on-rails open-uri


【解决方案1】:

NET::SSH.sftp.download 中有一个错误。使用 net/scp

require 'net/ssh'
require 'net/sftp'
require 'net/scp'

 begin

    puts '------------------------'
    puts 'Connecting to server...'

    Net::SSH.start(host, username, :password => password, :port => port) do |ssh|

      puts '------------------------'
      puts 'Entered the server...'

      puts '------------------------'
      puts 'Listing directory..'

      puts ssh.sftp.dir.entries(directory.to_s).map { |e| e.name }

      puts local_filename_path = Dir.pwd+'/'+filename.to_s

      puts remote_filename_path = "directory.to_s+filename.to_s"

      puts '------------------------'
      puts "Download file #{remote_filename_path}..."

      ssh.scp.download!(remote_filename_path, local_filename_path ) do |ch, name, sent, total|
        puts "#{name}: #{sent}/#{total}"
      end
      puts '------------------------'
      puts "File download #{remote_filename_path}!!"
      puts '------------------------'


      puts "done! exit!"

      ssh.wait!
      # ssh.execute! 'exit'
      ssh.close

    end

  rescue Timeout::Error
    @error = "  Timed out"
  rescue Errno::EHOSTUNREACH
    @error = "  Host unreachable"
  rescue Errno::ECONNREFUSED
    @error = "  Connection refused"
  rescue Net::SSH::AuthenticationFailed
    @error = "  Authentication failure"
  end

  if defined?(@error) then
    puts @error
  end

【讨论】:

    最近更新 更多