Home > Net >  How to unit test an Update method with no return using xUnit
How to unit test an Update method with no return using xUnit

Time:02-10

I have the follow method in the Repository class:

public async Task<int> Update(Entity entity)
{
    string sql = "UPDATE table SET name = @name";
    return await ExecuteAsync(sql, entity);
}

So, how can I do the unit test in this method, to ensure the record has been changed in the database ? I need to use mock or don't apply in this case ?

CodePudding user response:

Right now your code looks like untestable. To make it testable in this method you pasted, ExecuteAsync needs to be a mockable method of some interface injected in your service under test. Then after execution of Update method, you can verify that ExecuteAsync was called with the statement "UPDATE table SET name = @name"

  •  Tags:  
  • Related