2013-08-15 4 views
0

У меня есть следующий код:Nokogiri скребковый тест не работает?

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_hash %> в шаблоне я получаю это:

{: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):

app_spec.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)>' 

EDIT:

Я попытался это:

expect(@department_hash[:department]["Pet Supplies"].keys).to eq '["Birds", "Cats", "Dogs", "Fish & Aquatic Pets", "Horses", "Insects", "Reptiles & Amphibians", "Small Animals"]'

Но тест терпит неудачу также:

2) Department should scrap correct keys Failure/Error: expect(@department_hash[:department]["Pet Supplies"].keys).to eq '["Birds", "Cats", "Dogs", "Fish & Aquatic Pets", "Horses", "Insects", "Reptiles & Amphibians", "Small Animals"]' NoMethodError: undefined method []' for nil:NilClass # ./app_spec.rb:9:in block (2 levels) in '

Что может быть причиной ?

ответ

1

@department_hash не определен в рамках испытания.

Если взять простой пример:

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_hash определяется в основном, но не определена в тесте.

Вам необходимо запустить код приложения из сферы действия теста. Например, перемещение кода в тест, @department_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' (т.е. вы хотите сравнить хэши, а не строки в хэш

Update - Код экстрагированных к классу:.

Перемещение кода приложения в тест не является идеальным, если оно предназначено для повторного использования в другом месте. Вместо этого может быть лучше создать метод или класс для вашего кода приложения, а затем вызвать этот код из теста.

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 
+0

Есть ли способ сделать это, не перемещая весь мой файл на тест? Вот почему я включил его наверху. – alexchenco

+0

Вы можете извлечь код приложения в методы и/или классы (они могут находиться в файле app.rb). Затем из теста вы будете называть эти методы/классы. В ответ добавлен быстрый пример. –

+0

Спасибо большое! Я попробую и скажу вам результаты. – alexchenco

Смежные вопросы