I am attempting to write unit tests for an existing .NET MVC 4.5 web application that uses Entity Framework. I'd like to test that a bool method returns true/false as expected; however the method under test writes to a database log.
Unfortunately, these tests throw the following exception:
System.InvalidOperationException: No connection string named 'xxxDbContext' could be found in the application config file.
I do not want the database to be modified, and I'm struggling to find a sample of how to mock the `DbContext in this example.
How can I test only the boolean results without actually writing to the database? In the sample below, Logger.Log() adds a row to the database, and I don't want the actual database being modified during unit tests.
Here are simplified examples of what the code looks like:
Test method
[TestMethod]
public void LocalFileCompare_File2IsNull_ReturnsFalse()
{
var t = new tManager(new tSet());
FileInfo file1 = new FileInfo(@"C:\Temp\TempFile.txt");
FileInfo file2 = null;
var result = transferSet.LocalFileCompare(file1, file2);
Assert.IsFalse(result);
}
Method under test
public bool LocalFileCompare(FileInfo file1, FileInfo file2)
{
if (file1 == null || file2 == null)
{
Logger.Log("LocalFileCompare: One or both files are null.");
return false;
}
if (file1.FullName != file2.FullName)
{
Logger.Log($"LocalFileCompare - file names don't match.");
return false;
}
if (file1.Length != file2.Length)
{
Logger.Log($"LocalFileCompare - file sizes don't match.");
return false;
}
return true;
}
Thank you!
Logger?