Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Nokogiri刮削试验失败?_Ruby_Nokogiri_Rspec2 - Fatal编程技术网

Ruby Nokogiri刮削试验失败?

Ruby Nokogiri刮削试验失败?,ruby,nokogiri,rspec2,Ruby,Nokogiri,Rspec2,我有以下代码: url = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpets&field-keywords=" data = Nokogiri::HTML(open(url)) department = data.css('#ref_2619534011') @department_hash = {} department.css('li').drop(1).e

我有以下代码:

  url       = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpets&field-keywords="
  data      = Nokogiri::HTML(open(url))

  department   = data.css('#ref_2619534011')

  @department_hash = {}
  department.css('li').drop(1).each do | department |
    department_title = department.css('.refinementLink').text
    department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
    @department_hash[:department] ||= {}
    @department_hash[:department]["Pet Supplies"] ||= {}
    @department_hash[:department]["Pet Supplies"][department_title] = department_count
  end 
因此,当我在模板中执行此操作时,我得到以下结果:

{:department=>{"Pet Supplies"=>{"Birds"=>15918, "Cats"=>245418, "Dogs"=>513869, "Fish & Aquatic Pets"=>47182, "Horses"=>14774, "Insects"=>358, "Reptiles & Amphibians"=>5834, "Small Animals"=>19806}}} 
我为
app.rb
创建了一个规范(我使用的是Sinatra):

应用程序规范rb:

require File.dirname(__FILE__) + '/app.rb'

describe "Department" do
  it "should scrap correct string" do
    expect(@department_hash).to eq '{:department=>{"Pet Supplies"=>{"Birds"=>15918, "Cats"=>245418, "Dogs"=>513869, "Fish & Aquatic Pets"=>47182, "Horses"=>14774, "Insects"=>358, "Reptiles & Amphibians"=>5834, "Small Animals"=>19806}}}' 
  end
end
但测试失败了:

  1) Department should scrap correct string
     Failure/Error: expect(@department_hash).to eq '{:department=>{"Pet Supplies"=>{"Birds"=>15918, "Cats"=>245418, "Dogs"=>513869, "Fish & Aquatic Pets"=>47182, "Horses"=>14774, "Insects"=>358, "Reptiles & Amphibians"=>5834, "Small Animals"=>19806}}}'

       expected: "{:department=>{\"Pet Supplies\"=>{\"Birds\"=>15918, \"Cats\"=>245418, \"Dogs\"=>513869, \"Fish & Aquatic Pets\"=>47182, \"Horses\"=>14774, \"Insects\"=>358, \"Reptiles & Amphibians\"=>5834, \"Small Animals\"=>19806}}}"
            got: nil

       (compared using ==)
     # ./app_spec.rb:5:in `block (2 levels) in <top (required)>'
1)部门应废弃正确的字符串
失败/错误:expect(@department_hash).to eq{:department=>{“宠物用品”=>{“鸟”=>15918,“猫”=>245418,“狗”=>513869,“鱼和水生宠物”=>47182,“马”=>14774,“昆虫”=>358,“爬行动物和两栖动物”=>5834,“小动物”=>19806}
预计:“{:部门=>{“宠物用品”=>{“鸟类”=>15918,“猫”=>245418,“狗”=>513869,“鱼和水生宠物”=>47182,“马”=>14774,“昆虫”=>358,“爬行动物和两栖动物”=>5834,“小动物”=>19806}”
得:零
(使用==进行比较)
#./附录规范rb:5:in‘分块(2层)in’
编辑:

require File.dirname(__FILE__) + '/app.rb'

describe "Department" do
  it "should scrap correct string" do
    expect(@department_hash).to eq '{:department=>{"Pet Supplies"=>{"Birds"=>15918, "Cats"=>245418, "Dogs"=>513869, "Fish & Aquatic Pets"=>47182, "Horses"=>14774, "Insects"=>358, "Reptiles & Amphibians"=>5834, "Small Animals"=>19806}}}' 
  end
end
我试过这个:

expect(@department_hash[:department][“宠物用品”].键)。到eq “[“鸟”、“猫”、“狗”、“鱼和水生宠物”、“马”、“昆虫”, “爬行动物和两栖动物”、“小动物”]'

但测试也失败了:

2) 部门应报废正确的钥匙 失败/错误:expect(@department_hash[:department][“Pet Supplies”].键)。到eq'[“鸟”、“猫”、“狗”、“鱼和水生物” 宠物、马、昆虫、爬行动物和两栖动物、小动物]' 命名错误: nil:NilClass的未定义方法
[]
#./app_spec.rb:9:in
块(2层)in'


原因可能是什么?

@department\u hash
未在测试范围内定义

require 'rspec/autorun'
require 'nokogiri'
require 'open-uri'

# This class could be placed in your app.rb  
class DepartmentScraper
  def scrape_page()
    url       = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpets&field-keywords="
    data      = Nokogiri::HTML(open(url))

    department   = data.css('#ref_2619534011')

    @department_hash = {}
    department.css('li').drop(1).each do | department |
      department_title = department.css('.refinementLink').text
      department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
      @department_hash[:department] ||= {}
      @department_hash[:department]["Pet Supplies"] ||= {}
      @department_hash[:department]["Pet Supplies"][department_title] = department_count        
    end

    return @department_hash
  end
end

describe "Department" do
  it "should scrap correct string" do
    department_hash = DepartmentScraper.new.scrape_page()

    expect(department_hash).to eq({:department=>{"Pet Supplies"=>{"Birds"=>17556, "Cats"=>245692, "Dogs"=>516246, "Fish & Aquatic Pets"=>47424, "Horses"=>15062, "Insects"=>358, "Reptiles & Amphibians"=>5835, "Small Animals"=>19836}}})
  end
end
举个简单的例子:

require 'rspec/autorun'

@department_hash = 2
puts defined?(@department_hash)
#=> "instance-variable"

describe "Department" do
  it "should scrap correct string" do
        puts defined?(@department_hash)
        #=> "" (ie not defined)
    end
end
您可以看到,
@department\u hash
在main中定义,但它没有在测试中定义

require 'rspec/autorun'
require 'nokogiri'
require 'open-uri'

# This class could be placed in your app.rb  
class DepartmentScraper
  def scrape_page()
    url       = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpets&field-keywords="
    data      = Nokogiri::HTML(open(url))

    department   = data.css('#ref_2619534011')

    @department_hash = {}
    department.css('li').drop(1).each do | department |
      department_title = department.css('.refinementLink').text
      department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
      @department_hash[:department] ||= {}
      @department_hash[:department]["Pet Supplies"] ||= {}
      @department_hash[:department]["Pet Supplies"][department_title] = department_count        
    end

    return @department_hash
  end
end

describe "Department" do
  it "should scrap correct string" do
    department_hash = DepartmentScraper.new.scrape_page()

    expect(department_hash).to eq({:department=>{"Pet Supplies"=>{"Birds"=>17556, "Cats"=>245692, "Dogs"=>516246, "Fish & Aquatic Pets"=>47424, "Horses"=>15062, "Insects"=>358, "Reptiles & Amphibians"=>5835, "Small Animals"=>19836}}})
  end
end
您需要在测试范围内运行应用程序代码。例如,将代码移动到测试中,
@department\u hash
将不再为零

require 'rspec/autorun'
require 'nokogiri'
require 'open-uri'

describe "Department" do
  it "should scrap correct string" do
    url       = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpets&field-keywords="
    data      = Nokogiri::HTML(open(url))

    department   = data.css('#ref_2619534011')

    @department_hash = {}
    department.css('li').drop(1).each do | department |
      department_title = department.css('.refinementLink').text
      department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
      @department_hash[:department] ||= {}
      @department_hash[:department]["Pet Supplies"] ||= {}
      @department_hash[:department]["Pet Supplies"][department_title] = department_count
    end       

    expect(@department_hash).to eq({:department=>{"Pet Supplies"=>{"Birds"=>17556, "Cats"=>245692, "Dogs"=>516246, "Fish & Aquatic Pets"=>47424, "Horses"=>15062, "Insects"=>358, "Reptiles & Amphibians"=>5835, "Small Animals"=>19836}}})
  end
end
请注意,您的测试应该是
eq(hash)
,而不是
eq“hash”
(即您想要比较hash而不是字符串与hash)

更新-提取到类的代码:

如果要在其他地方重用应用程序代码,则将其移动到测试中并不理想。相反,最好为应用程序代码创建一个方法或类,然后从测试中调用该代码

require 'rspec/autorun'
require 'nokogiri'
require 'open-uri'

# This class could be placed in your app.rb  
class DepartmentScraper
  def scrape_page()
    url       = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpets&field-keywords="
    data      = Nokogiri::HTML(open(url))

    department   = data.css('#ref_2619534011')

    @department_hash = {}
    department.css('li').drop(1).each do | department |
      department_title = department.css('.refinementLink').text
      department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
      @department_hash[:department] ||= {}
      @department_hash[:department]["Pet Supplies"] ||= {}
      @department_hash[:department]["Pet Supplies"][department_title] = department_count        
    end

    return @department_hash
  end
end

describe "Department" do
  it "should scrap correct string" do
    department_hash = DepartmentScraper.new.scrape_page()

    expect(department_hash).to eq({:department=>{"Pet Supplies"=>{"Birds"=>17556, "Cats"=>245692, "Dogs"=>516246, "Fish & Aquatic Pets"=>47424, "Horses"=>15062, "Insects"=>358, "Reptiles & Amphibians"=>5835, "Small Animals"=>19836}}})
  end
end

有没有办法不在测试中移动我所有的文件就可以做到这一点?这就是为什么我把它放在顶部。你可以将应用程序代码提取到方法和/或类中(这些可能位于你的app.rb文件中)。然后从测试中,您可以调用这些方法/类。答案中添加了一个快速示例。非常感谢!我将尝试它并告诉您结果。感谢它起作用。顺便问一下,
rspec/autorun
部分是关于什么的?自动包含
app.rb
?通常您会使用
rspec之类的命令来运行您的规范您的_spec.rb
。通过要求
rspec/autorun
,您的测试将始终在包含文件时运行-即在您只使用
ruby您的_spec.rb
时运行。您可能不需要
autorun
。但是,对于一次性脚本或使用类似SciTE的编辑器,我发现使用
autorun
更有效我很方便。