Possibility to assert invoked Uris?
Opened this issue · 4 comments
Hi,
I'm using your library to author some tests and I got myself in the situation where my API client is forming a URL by adding many components both in the path and the querystring.
My test is targeting only a subsection of the requested URI and I would like to "test" it. Ideally on the used option.
To make a concrete example
var option = new HttpMessageOptions
{
HttpMethod = HttpMethod.Get,
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonConvert.SerializeObject(contact))
},
RequestUri = null
};
var sut = CreateSystemUnderTest(option);
var response = await sut.GetByIdAsync(contactId);
Assert.That(option.InvokedUri, Contains.SubString($"/contacts/v1/contact/vid/{contactId}"));
Right now I am forced to do:
var options = new HttpMessageOptions
{
HttpMethod = HttpMethod.Get,
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonConvert.SerializeObject(contact))
},
RequestUri = new Uri($"https://api.hubapi.com/contacts/v1/contact/vid/{contactId}/profile?formSubmissionMode=all&propertyMode=value_and_history&showListMemberships=true")
};
which is really outside of the scope of my unit test.
Hi again @Kralizek 👋
Hmm .. lets see here ...I first added this way back in #12 over here.
I just need to add some documentation to show how to do this.
In the mean time, here's an example using your code-example, above:
// Act.
var response = await sut.GetByIdAsync(contactId);
// Assert.
Assert.That(option.NumberOfTimesCalled, 1);
So what is happening is that we check that all the conditions of the Options
was met and passed and the number of times it was met.
Does this help?
I'm not sure it would help my case (although I barely remember what I was working on back then).
I guess this would go in hand with the default registration we are discussing in #32.
Let RequestUri
be optional and allow the test on the actual call.
Back to the first snippet, you could read its assertion as "I will receive a GET request whose URI will contain a subscring matching this pattern".
Currently, options.RequestUri
is optional. 👍
"I will receive a GET request whose URI will contain a substring matching this pattern".
kewl! to do that, then you do exactly as you did, above ... and make then test that the option you just made, was called :)
which is the code I did above. Kewl!
Thanks! I'll check later :)