I am working on an application that includes a web scraper for gathering data and would like to verify that a particular service (which in the future will grow to several services) is performing the correct business logic given an expected HTML DOM tree is returned to it.
Rather than performing actual HTTP requests each time a test is run, I would prefer to "mock" this out by providing a static document for the test and returning a pre-defined HTML document. I'd prefer instead for my unit test to reflect "Given this HTML document, verify the output is correct business wise" which does not need to include AngleSharp's HTTP request.
Here's how I load the document now, which I have placed into a "wrapper" service that I can then inject into my service through dependency injection:
var angleSharpConfig = Configuration.Default.WithDefaultLoader();
var angleSharpContext = BrowsingContext.New(angleSharpConfig);
var document = await angleSharpContext.OpenAsync(url);
I see that there is a LoaderOptions
object that can be passed to WithDefaultLoader
, but it does not seem to provide a way to mock the HTTP request. There does seem to possibly be a way to do this with the With
method on the default configuration object, however I am struggling to see how to sanely do this.
Other suggestions for alternate approaches are welcome, as well - due to my inexperience I may be attempting to skin a cat that needn't be skinned at all.