2015-09-29 34 views

回答

0

它看起来像多OpenURI你需要比引入nokogiri本身存根。

你可以很容易地用Mocha宝石。

fake_html = <<-EOS 
    <html> 
    <head><title>Some Title</title></head> 
    <body>Some Content</body> 
    </html> 
EOS 
PromoDate.any_instance.stubs(:open).returns(fake_html) 

您也可以确保在该试验表明,该函数被调用合适的参数:

PromoDate.any_instance.expects(:open).with('http://www.google.com').returns(fake_html) 
0

在这里,你不仅要存根引入nokogiri :: HTML也是“开放式”的方法,所以你可以做水木清华这样的:

expect(PromoDate).to receive(:open).and_return(File.new("#{Rails.root}/spec/fixtures/page_for_parsing.html")) 
expect(Nokogiri::HTML::Document).to receive(:parse).and_return(whatever_you_want) 

凡page_for_parsing.html的html页面中灯具创建的文件夹用于测试目的。 Nokogiri :: HTML只是Nokogiri :: HTML :: Document.parse的一个简便方法,你可以在source code