Just started learning and writing unit testing a day ago so this is probably too simple question:
I have this method in my DBTaskHanlder
class that I want to do some unit for, I was able to write one for when ModelState is not valid but now for next one:
public bool CreateTask(ForgotPasswordViewModel fpModel)
{
if (!ModelState.IsValid)
{
return false;
}
try
{
CreateTaskFromModel(fpModel);
_dbContext.SaveChanges();
return true;
}
catch (Exception e)
{
var issue = e.ToString();
throw;
}
}
That CreateTaskFromModel
is a private
method and well called its job is to create a new row in database on a table.
So I wanted to test when this method is called is one new row getting created in DB?
Is it actually the correct thing to test? How to test ? I don't think we should hit and insert into the real database right?
private void CreateTaskFromModel(ForgotPasswordViewModel fpModel)
{
var message = _dbContext.Create<Message>();
message.MessageType = "TASK".PadLeft(10);
message.Assigned_User_K = fpModel.SendPasswordRequestTo.Trim();
message.Assigned_Date = DateTime.Today;
message.Source_User_K = string.Empty;
message.Target_File_K = "WEBCFGPHRM";
message.Owner_User_K = string.Empty;
message.Message_K = _keyGenerator.Get10ByteBase36Key();
_dbContext.Messages.Add(message);
}