C
C#11mo ago
Raki

Need help on how to write unit tests in xunit for the code

c#
public async Task UpdateEntryAsync(string contentTypeId, string entryId, int? version, string endpoint)
{
var entryRequest = await _httpClient.GetAsync($"{endpoint}/{entryId}");
var entryContent = await entryRequest.Content.ReadAsStringAsync();
dynamic getEntry = JsonConvert.DeserializeObject(entryContent);

//Update the PreviousSlug with current slug
getEntry.fields.previousSlug = getEntry.fields.slug;
var updatedEntryJson = JsonConvert.SerializeObject(getEntry);
var updatedEntryContent = new StringContent(updatedEntryJson, Encoding.UTF8, "application/json");

// Set headers for update request
updatedEntryContent.Headers.Add("X-Contentful-Content-Type", contentTypeId);
updatedEntryContent.Headers.Add("X-Contentful-Version", version.ToString());

await _httpClient.PutAsync($"{endpoint}/{entryId}", updatedEntryContent);

version++;
updatedEntryContent.Headers.Remove("X-Contentful-Version");
updatedEntryContent.Headers.Add("X-Contentful-Version", version.ToString());
await _httpClient.PutAsync($"{endpoint}/{entryId}/published", updatedEntryContent);
}
c#
public async Task UpdateEntryAsync(string contentTypeId, string entryId, int? version, string endpoint)
{
var entryRequest = await _httpClient.GetAsync($"{endpoint}/{entryId}");
var entryContent = await entryRequest.Content.ReadAsStringAsync();
dynamic getEntry = JsonConvert.DeserializeObject(entryContent);

//Update the PreviousSlug with current slug
getEntry.fields.previousSlug = getEntry.fields.slug;
var updatedEntryJson = JsonConvert.SerializeObject(getEntry);
var updatedEntryContent = new StringContent(updatedEntryJson, Encoding.UTF8, "application/json");

// Set headers for update request
updatedEntryContent.Headers.Add("X-Contentful-Content-Type", contentTypeId);
updatedEntryContent.Headers.Add("X-Contentful-Version", version.ToString());

await _httpClient.PutAsync($"{endpoint}/{entryId}", updatedEntryContent);

version++;
updatedEntryContent.Headers.Remove("X-Contentful-Version");
updatedEntryContent.Headers.Add("X-Contentful-Version", version.ToString());
await _httpClient.PutAsync($"{endpoint}/{entryId}/published", updatedEntryContent);
}
3 Replies
Raki
RakiOP11mo ago
I'm currently learning unit tests and need to know how to write test for the following method. It doesn't return anything just doing a action. How to asset that. I'm clueless on this one.
nuthanm
nuthanm11mo ago
@Raki - please find the below code and hope it solves your problem. [Fact] public async Task UpdateEntryAsync_ShouldUpdateAndPublish() { // Arrange var contentTypeId = "your_content_type_id"; var entryId = "your_entry_id"; var version = 1; // replace with your actual value var endpoint = "your_api_endpoint"; // you can give any dummy here var httpClientMock = new Mock<IHttpClient>(); var entryService = new EntryService(httpClientMock.Object); // Mocking the response when fetching the entry var entryResponseContent = @"{ 'fields': { 'slug': 'new-slug', 'previousSlug': 'old-slug' } }"; httpClientMock.Setup(client => client.GetAsync($"{endpoint}/{entryId}")) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(entryResponseContent, Encoding.UTF8, "application/json") }); // Act await entryService.UpdateEntryAsync(contentTypeId, entryId, version, endpoint); // Assert httpClientMock.Verify(client => client.PutAsync($"{endpoint}/{entryId}", It.IsAny<StringContent>()), Times.Exactly(2)); httpClientMock.Verify(client => client.PutAsync($"{endpoint}/{entryId}/published", It.IsAny<StringContent>()), Times.Once); } } Here I used MOQ library to complete this. We don’t perform actual http call but we configured or setup the response so that it mimics the call and take the defined response and process it. Hope you understand this
Pobiega
Pobiega11mo ago
Methods that don't return anything are hard to test and you have more or less no option but to use mocking/fakes to just verify that it calls the expected methods. Nuthanms code above does this, even thou the actual assertions seem wrong at a glance
Want results from more Discord servers?
Add your server