我正在使用 JustMock 和 Entity Framework 来尝试为服务编写单元测试。在服务中,我有这个方法:
List<Log> GetLogType(string type)
{
using (var db = new LogContext())
{
return db.Logs.Where(x => x.Type == type).ToList();
}
}
我有一个测试:
[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());
}
如何让服务使用我的模拟上下文?现在它正在尝试连接到数据库,因此出错。