I'm using JustMock and Entity Framework to try to write unit tests for a service. In the service, I have this method:
List<Log> GetLogType(string type)
{
using (var db = new LogContext())
{
return db.Logs.Where(x => x.Type == type).ToList();
}
}
And I have a test:
[TestMethod]
public void GetLogTypeTest()
{
IList<Log> logs = new List<Log>
{
new Log() {
Id = 1,
Type = "Debug",
Message = "Test Message"
}
};
var logContext = Mock.Create<LogContext>(Constructor.Mocked).PrepareMock();
logContext.Logs.Bind(logs);
var service = new LogService();
var debugs = service.GetLogType("Debug");
Assert.AreEqual(1, debugs.Count());
}
How do I get the service to use my mocked context? Right now it is trying to connect to the database, and thus erroring.
REASON
This is not happened because of
JustMock
. It happened because you creating new instance ofLogContext
in your method. Your method is not unit testable because it will always create new instance of service and establish connection with real database. There are several dependency injection frameworks suitable for C# please review Unity if you are interested.TO FIX THIS
You have to use dependency injection and inject your service in class. In that case you can avoid connection to database. Your method will be like this:
_logContext
is global variable ofLogContext
type which is injected trough constructor.Than you can mock it and pass mock trough constructor.
Example of class can be:
Now you can create test case like in following:
Please note what I did. I created
service
instance like you but I injected mocked instance of real service trough constructor. Mocked instance will never connect to database, it will return data which you provided in configuration.