Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: uninitialized constant ::Nokogiri

I wrote test for model:

describe Video do
  describe 'searching youtube for video existence' do
    it 'should return true if video exists' do
      Video.video_exists?("http://www.youtube.com/watch?v=KgfdlZuVz7I").should be_true
    end
  end  
end

Here is model code:

class Video < ActiveRecord::Base
  attr_accessible :video_id

  def self.video_exists?(video_url)
    video_url =~ /\?v=(.*?)&/
    xmlfeed = Nokogiri::HTML(open("http://gdata.youtube.com/feeds/api/videos?q=#{$1}"))
    if xmlfeed.at_xpath("//openSearch:totalResults").content.to_i == 0
      return false
    else
      return true
    end
  end
end

But it fails with error:

Failures:

  1) Video searching youtube for video existence should return true if video exists
     Failure/Error: Video.video_exists?("http://www.youtube.com/watch?v=KgfdlZuVz7I").should be_true
     NameError:
       uninitialized constant Video::Nokogiri
     # ./app/models/video.rb:6:in `video_exists?'
     # ./spec/models/video_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.00386 seconds
1 example, 1 failure

I don't know how to solve this, what could be problem?

like image 308
Иван Бишевац Avatar asked Dec 12 '22 22:12

Иван Бишевац


1 Answers

Problem was because I didn't add gem nokogiri to Gemfile.

After adding it I removed require 'nokogiri' and require 'open-uri' from model and it works.

like image 188
Иван Бишевац Avatar answered Dec 20 '22 07:12

Иван Бишевац