Add support for unit tests with an example.#5
Conversation
|
Going to hold off on this for a bit. Plus, I'm not quite sure yet which unit testing framework I'm going to use (although xUnit was what I was leaning towards). |
|
Once I decide on the unit test framework, I may just start adding tests using Tests for the rest of the bits (including potentially mocking the client) can then come later. |
| *.*proj.user | ||
| *.DotSettings.user | ||
| *.userprefs | ||
| /_ReSharper.Caches |
There was a problem hiding this comment.
Odd, I've never seen R# create that folder. But then I'm mostly using Rider, so this might be something only the VS plugin does.
There was a problem hiding this comment.
Yeah, I have my ReSharper configured that way so it gets cleaned up regularly.
| public class BrowseTests : MusicBrainzTest { | ||
|
|
||
| private const string ReleaseTestData = @"{ | ||
| ""releases"":[ |
There was a problem hiding this comment.
I might be more inclined to use a .json file stored as embedded resource; the overhead of loading that is small enough, and it makes the test data easier to work with.
There was a problem hiding this comment.
Agreed - I was thinking this would be a better path to go down but kept the POC simple.
Yeah, that would be ideal for unit testing - break it down into the components and test each phase. There may also be merit in testing that it generates the same serialized JSON requests and URLs between builds with unit tests as well. |
HttpClient would be great. Just watch for the usual HttpClient pit-falls where it doesn't clean up well. Simplest is to provide a constructor for your Query with HttpClient exposes and then they can simply configure .NET Core DI (or similar) to use something like the standard IServiceCollection.AddHttpClient which will automatically inject an appropriate HttpClient instance. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1 |
|
Yes, that's in part why I suggested avoiding using many short-lived Query objects :) |
I've created a strategy for mocking the responses to the WebClient class and added a sample unit test on the browse releases. Should be relatively easy to build out more tests from here. Let me know what you think.