我正在尝试使用NUnit和Moq编写单元测试。我在我的项目中使用LINQ to SQL。
首先,我创建了一个与本博客中的包装类相似的包装类
http://andrewtokeley.net/archive/2008/07/06/mocking-linq-to-sql-datacontext.aspx
这是我的代码
[Test]
public void MakeModelsListTest()
{
var objTbl1 = new List<tblSomeTable>
{
new tblSomeTable { Make = "Nissan", Model = "Rogue"},
new tblSomeTable { Make = "Hyundai", Model= "Elantra GT"},
new tblSomeTable { Make = "Ram", Model="Ram Pickup 3500"},
new tblSomeTable { Make = "Ford", Model="Freestar"},
new tblSomeTable { Make = "Chrysler", Model="Aspen Hybrid"},
new tblSomeTable { Make = "Buick", Model="Verano"}
}.AsQueryable();
var objDataContext = new Mock<DataContextWrapper<CarvanaDataContext>>();
var dbContext = objDataContext.As<IDataContextWrapper>();
dbContext.Setup(i => i.Table<tblSomeTable>()).Returns(objTbl1);
MpcController objMpc = new MpcController();
AutoTraderMakeModels makeModels = objMpc.MakeModels( objDataContext.Object);
Assert.That(makeModels, Is.InstanceOf(typeof(AutoTraderMakeModels)));
Assert.That(makeModels.results.Count(), Is.GreaterThan(0));
Assert.That(makeModels.results[0].models.Count(), Is.GreaterThan(0));
}
在这一行
AutoTraderMakeModels makeModels = objMpc.MakeModels( objDataContext.Object);
我收到此错误
“System.Collections.Generic.KeyNotFoundException”类型的异常 发生在mscorlib.dll中但未在用户代码中处理
附加信息:给定的密钥不在 字典。
我不确定我做错了什么。我是Moq的新手。请提出建议!