Unit testing for async method with boolean return type
first100
81
Reputation points
Hi,
Im deveolping a set of unit test for my project, using xunit.
In my code I have an handler that receive messages from kafka.
This handler deserialize message to an entity and write it on sql database and returning true or false if some rows are written on database.
I have a mocked method that return boolean and i want to test it, but i cannot assert to true or false because I receive always an object when i have to do Assert stage , im expect a boolean value; code below and thanks for whoever will help me.
[Fact]
[Unit]
public async Task MessageReceivedNoRowsAffected()
{
//Arrange
var cancellation = new CancellationToken();
Message value = new Message()
{
type = "test",
code = "123456",
name = "name",
};
dynamic Message = new System.Dynamic.ExpandoObject();
Message.id = "1";
Message.codetype = "jjj";
Message.time = DateTime.Now;
Message.pref = "pref";
Message.version = 1;
Message.type = "test";
Message.code = "123456";
Message.value = value;
var ret = await Task.Run ( () => HandlerMock.Setup(m => m.EventReceived(value, cancellation))
.Returns(PrjTests.MockedRepo.HandlerMock.
MessageReceivedNoInsert(value, cancellation))
);
Assert.False(ret,"This is false"); // Dont work ret is an object here
}
public static class HandlerMock
{
public static async Task<bool> MessageReceivedRecordInserted(Message message,CancellationToken token)
{
var result = true ;
// simulate return true if rows affected is > 0
return await Task.FromResult(
true
);
}
public static async Task<bool> MessageReceivedNoInsert(PreferenceEventMessage message, CancellationToken token)
{
// simulate no records affected
var result = false;
return await Task.FromResult(
result
);
}
Community Center Not monitored
46,183 questions
Sign in to answer