Search code examples
c#unit-testingtddscrapysharp

Mocking ScrapySharp response for unit test


I'm using ScrapySharp in my clean architecture solution and I need to mock a Scraping service response in my unit tests so that the unit test is self contained and not actually hitting any external server.

I've looked at using Moq but don't see a way to return a new ScrapySharp WebPage for my code then to use during the unit test.

I have an interface for my service in my infrastructure project:

public interface IScrapeService
{
    WebPage NavigateToPage(Uri url, HttpVerb verb = HttpVerb.Get, string data = "", string contentType = null);
}

I then would have my real world implementation of this service that would get injected into the class which uses the service. My unit test would use a mocked implementation of the service that would get injected during my test.

Has anybody attempted anything like this or can help me if I'm not thinking about this properly?


Solution

  • The question here is: Why do you need to expose ScrapySharp's response as output value of your method? May be it is a better to return some custom model which will contain only required information from ScrapySharp's response. Custom model will give one more advantage - all clients of your IScrapeService interface will not need to have explicit reference to ScrapySharp. And, of course, it will give you better abilities for testing of this interface.