Ruby Nokogiri RSpec-套接字错误:无法打开TCP连接-getaddrinfo:提供了节点名或服务名

问题描述

我正在使用Nokogiri从One Anime维基页面获取信息。我编写了一些测试,以检查我存储在.txt文件中的过去的碎片,因为Wiki每隔几个月左右就会更新其页面上的信息。

当我分别运行它们时,所有RSpec测试都会通过。一次运行所有应用程序RSpec测试会使所有Scraper测试失败。

如果我自己运行所有刮板测试:

Scraper
  instance methods
    #get_all_characters scrapes a list of characters
    #get_all_fruits scrapes a list of fruits
    #get_fruits_info scrapes devil fruit info
    #get_summary scrapes the show summary
    #get_haki scrapes Haki @R_985_4045@ion
    #get_episode_list scrapes how many episodes there are

Finished in 6.04 seconds (files took 0.60889 seconds to load)
6 examples,0 failures

所有测试都会通过。但是,如果我运行spec文件夹中的每个测试,则所有测试都会失败,并显示相同的错误

Scraper
  instance methods
    #get_all_characters scrapes a list of characters (Failed - 1)
    #get_all_fruits scrapes a list of fruits (Failed - 2)
    #get_fruits_info scrapes what devil fruits are (Failed - 3)
    #get_summary scrapes the show summary (Failed - 4)
    #get_haki scrapes @R_985_4045@ion about Haki (Failed - 5)
    #get_episode_list scrapes how many episodes there are (Failed - 6)

Failures:

  1) Scraper instance methods #get_all_characters scrapes a list of characters
     Failure/Error: page = Nokogiri::HTML(URI.open(site))
 
     SocketError:
       Failed to open TCP connection to onepiece.fandom.comhttps:443 (getaddrinfo: nodename nor servname provided,or not kNown)
   # ./lib/classes/scraper.rb:99:in `grab_bio'
   # ./lib/classes/scraper.rb:106:in `block in all_character_bios'
   # ./lib/classes/scraper.rb:105:in `each'
   # ./lib/classes/scraper.rb:105:in `all_character_bios'
   # ./lib/classes/scraper.rb:40:in `get_all_characters'
   # ./lib/classes/scraper.rb:6:in `initialize'
   # ./spec/cli/scraper_spec.rb:6:in `new'
   # ./spec/cli/scraper_spec.rb:6:in `block (2 levels) in <top (required)>'
   # ------------------
   # --- Caused by: ---
   # SocketError:
   #   getaddrinfo: nodename nor servname provided,or not kNown
   #   ./lib/classes/scraper.rb:99:in `grab_bio'

为什么要运行所有测试 导致套接错误? Scraper规范测试是唯一测试Scraper类的工具。没有其他规范测试会实例化Scraper类的实例。我似乎不知道为什么会这样。

以前有人遇到过此错误吗?为什么会发生?

刮板规格测试回购链接 https://github.com/GoodGuyGuf/One_Piece/blob/main/spec/cli/scraper_spec.rb

项目仓库: https://github.com/GoodGuyGuf/One_Piece

抓取者规范代码

require_relative "../spec_helper.rb"
describe Scraper do

  before(:context) do
    SCRAPER = Scraper.new
  end

  context "instance methods" do
    it "#get_all_characters scrapes a list of characters" do
      all_characters_scrape = ScraperHelper.obj_to_arr_of_hashes(SCRAPER.characters)
      old_character_read = eval(ScraperHelper.read_file("./data/characters.txt"))
      expect(all_characters_scrape).to eq(old_character_read)
    end

    it "#get_all_fruits scrapes a list of fruits" do
      all_fruits_scrape = ScraperHelper.obj_to_arr_of_hashes(SCRAPER.fruits)
      old_fruits_read = eval(ScraperHelper.read_file("./data/fruits.txt"))
      expect(all_fruits_scrape).to eq(old_fruits_read)
    end

    it "#get_fruits_info scrapes what devil fruits are" do
      expect(SCRAPER.fruits_info).to match(ScraperHelper.read_file("./data/fruits_info.txt"))
    end

    it "#get_summary scrapes the show summary" do
      expect(SCRAPER.summary).to match(ScraperHelper.read_file("./data/summary.txt"))
    end

    it "#get_haki scrapes @R_985_4045@ion about Haki" do
      expect(SCRAPER.haki).to match(ScraperHelper.read_file("./data/haki.txt"))
    end

    it "#get_episode_list scrapes how many episodes there are" do
    expect(SCRAPER.episode_list).to match(ScraperHelper.read_file("./data/episodes.txt"))
    end 
  end
end

我没有在上面的let中使用它,因为随后的测试两次将信息刮擦一次,因此创建了两倍的对象。通过使用before(:context),一旦确保所有测试均通过,便会设置Scraper实例,并将输出正确的信息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)