Jump to content
JamieR

DUnitX: How can I load HTML files within unit test?

Recommended Posts

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
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

After some research I've found some good ones:

  • DSharp
  • Delphi Mock
  • Spring4D Mocks

Share this post


Link to post

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.

  • Like 1
  • Thanks 1

Share this post


Link to post

+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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×