C#C
C#3y ago
4 replies
Raki

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

 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);
        }
Was this page helpful?