JamieR 3 Posted December 9, 2021 I’m writing a web scraper. The data I need is within script tag on the HTML page. I use a regular expression to extract the JavaScript object that’s within a script tag on the scraped page, and turns that into a JSON object. I’m also writing unit tests along the way using DUnitX. What I’d like to know is can I load some HTML files to test against within the unit test? I have a handful of HTML documents from the website that contain the data required, and some document that don’t. Instead of hitting the websites URL I want to mimic that response with the HTML files I have. How can I do this? Share this post Link to post
David Heffernan 2345 Posted December 9, 2021 You need a mock. There are lots of mocking frameworks around. Are you using one? Share this post Link to post
JamieR 3 Posted December 9, 2021 Just now, David Heffernan said: You need a mock. There are lots of mocking frameworks around. Are you using one? No. Any suggestions on a good one? Share this post Link to post
JamieR 3 Posted December 9, 2021 After some research I've found some good ones: DSharp Delphi Mock Spring4D Mocks Share this post Link to post
Anders Melander 1782 Posted December 9, 2021 If the logic that does the HTTP request, extract the HTML and scrapes the HTML are all nicely separated then I don't see why you'd need a mock (personally I try to avoid them as they just become one more piece of code to maintain). For example if your scraper reads from a stream, then just have the unit test pass it a TFileStream and Bob's your uncle/aunt/non-binary auncle. 1 1 Share this post Link to post
JamieR 3 Posted December 10, 2021 Gave me a good chuckle then Anders, ha ha. Share this post Link to post
Fr0sT.Brutal 900 Posted December 10, 2021 +1 to Anders. No need in running a car for the whole day just to check windshield wipers. But if you want to check the overall cycle, you can run any simple local web server and test against it Share this post Link to post