views:

145

answers:

1

Hi, I'm working with mechanize and having problems with inheritance when testing in a rails environment, using script/console.

When I write like this:

require 'rubygems'
require 'mechanize'

agent = WWW::Mechanize.new
agent.get 'http://www.google.com'

Everything works fine. But when I try to create a subclass of WWW::Mechanize like this:

require 'rubygems'
require 'mechanize'

class Alfa < WWW::Mechanize; end

agent = Alfa.new
agent.get 'http://www.google.com'

I get the following error:

NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.parse from /Library/Ruby/Gems/1.8/gems/mechanize-0.9.3/lib/www/mechanize/page.rb:77:in `parser'

Am I missing something?

Thanks in advance

+1  A: 

When you subclass WWW::Mechanize, no HTML parser is provided : that is what the error line actually tells you.

This works :

class Agent < WWW::Mechanize
end
a = Agent.new
a.html_parser = Nokogiri::HTML
a.get 'http://www.google.com'
i-blis
Thanks i-blis, I thought these setup steps were made automatically by the underlying Page class.
thiagobrandam