[RUBY] Test Nokogiri with Rspec.

The argument passed to Nokogiri is mocked and tested, so leave it.

FUGA_FUGA = ['Company', 'secret']
def hogegiri_method(fuga)
  HOGE_COUNT = 5
  HOGE_COUNT.times do |time|
    html = Nokogiri::HTML(OpenURI.open_uri(url))
    a, b, c =Regular expression messed up
    break if FUGAFUGA.include?(a)
  end
  { a: a, b: b, c: c }
end

It's a shit-like code because it's a code that can't be shown in various ways, but this time I want to create a fake return value as a mock.

html = Nokogiri::HTML(OpenURI.open_uri('secret'))

here

However, it is difficult to set the return value of Nokogiri directly with and_return.

--The return value is Nokogiri :: HTML :: Document, which does not return simple html. --The method is used inside the method, which can only be used for the return value peculiar to nokogiri.

Such

In other words, you have to let nokogiri do the work, but do not access the URL and get the desired data.

Then what to do

OpenUri.open_uri

Mock the return value of this and pass the value to be passed to the argument of Nokogiri.

It seems that the HTML you want to parse is included in the argument of Nokogiri. Then, in the production environment, you can copy the HTML of the URL you access and fetch, create an htmlfile, paste it there, and use it as the mock of the return value! !! I didn't write an article about how easy it was.

Final answer

spec/test.html


HTML copy
allow(OpenURI).to receive(:open_uri).and_return(File.new("#{Rails.root}/spec/test.html"))

Complete Now the method works as if you were accessing the url and fetching it. Furthermore, if you do the argument of ʻand_return like nil, File.new ("# {Rails.root} /spec/test.html")` and you can not get the value as the logic of the method created this time, it is up to 5 Since it loops times, you can also confirm that it is looping.

Since I started the internship, I'm doing things that I would never do in personal development, so I'm going to get stuck every time, but I'm trying to get somehow taught and sticky

May have grown a little

Recommended Posts

Test Nokogiri with Rspec.
[Rails] Test with RSpec
Test Active Strage with RSpec
Test GraphQL resolver with rspec
Test with RSpec + Capybara + selenium + chromedriver
Copy and paste test with RSpec
Let's unit test with [rails] Rspec!
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Model test with RSpec
I want to test Action Cable with RSpec test
I rewrote the Rails tutorial test with RSpec
RSpec test code execution
Integration Test with Gradle
[For beginners] Test devise user registration with RSpec
Introduction to RSpec 1. Test, RSpec
Testing model with RSpec
Use webmock with Rspec
Automatically test with Gauge
Load test with JMeter
Unit test with Junit.
Introduction to RSpec 4. Create test data with Factory Bot
Test Web API with junit
Check CSV value with RSpec
[Rails] Test code using Rspec
Remote debugging with Gradle test
[Rails] About Rspec response test
How to test a private method with RSpec for yourself
How to erase test image after running Rspec test with CarrierWave
Image upload using CarrierWave ~ Rspec test ~
test
REST API test with REST Assured Part 2
RSpec ~ Task model validation test creation
test
Output test coverage with clover + gradle
[Rails5] Rspec -Unit test when nesting-
test
About app testing RSpec (unit test)
test
[Java] Test private methods with JUnit
Test list inclusion relationships with AssertJ
[RSpec] How to write test code
Test Spring framework controller with Junit
[RSpec] Unit test (using gem: factory_bot)
MASCHINE test Linear Regression with Multiple Variables
Introduce RSpec and write unit test code
Java EE test (CDI / interceptor) with Arquillian
[Note] How to get started with Rspec
Perform transaction confirmation test with Spring Boot
How to test private scope with JUnit
JUnit 5 gradle test fails with lombok annotation
rails test fails with database reference error
Java automated test implementation with JUnit 5 + Gradle
How to write an RSpec controller test
Form class validation test with Spring Boot
[Rails] I want to test with RSpec. We support your step [Introduction procedure]