I have a function which saves to a couch db database and it is a Task Void method, it does not return anything and I want to unit test that, but it gives the error I am not expecting...
I've tried Asserting that the return is a Task type but it doesn't work
public async Task CreateMessageHistoryAsync(Message message)
{
//This is the method that I am testing
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException(await response.Content.ReadAsStringAsync());
}
//This will throw if it does not save, can I test this?
}
[Fact]
public async Task Should_NotThrowHttpRequestException_When_AMessageHistoryIsCreated()
{
var recipients = MockMessage.GetRecipients("Acc", "Site 1", "Site 2", "Site 3");
var message = MockMessage.GetMessage(recipients);
mockStateFixture
.MockMessageHistoryService
.Setup(service => service.CreateMessageHistoryAsync(message));
var messageHistoryService = new MessageHistoryService(
mockStateFixture.MockIHttpClientFactory.Object);
await Assert.IsType<Task>(messageHistoryService.CreateMessageHistoryAsync(message));
}
I expect a task to return but this is the actual results:
Result Message:
Assert.IsType() Failure
Expected: System.Threading.Tasks.Task
Actual: System.Threading.Tasks.Task`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
The Task returned from your async method is generated by the compiler. It may be an instance of Task, or it may be an instance of a derived type. And this may change with future updates. You don't want to check specifically for the Task type; that would tie your unit test to an implementation detail.
You could test for "is this Task or a derived type?", which looks like await Assert.IsAssignableFrom<Task>(messageHistoryService.CreateMessageHistoryAsync(message));
However, I don't think that's a useful assertion at all. It's not useful to assert that a method returning T actually returns a T (or derived type), because of course it returns T! So, I would recommend removing that assertion completely: await messageHistoryService.CreateMessageHistoryAsync(message);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With