Moq设置返回一些硬编码的POCO
本文关键字:编码 POCO 设置 返回 Moq | 更新日期: 2023-09-27 18:10:38
我试图采用这个链接来获得moq内部的一些对象来传递给我的服务。http://allen-conway-dotnet.blogspot.com/2014/01/creating-unit-test-using-moq-to-stub.html
我在Returns()函数上遇到错误,说没有满足此要求的重载函数。返回一个可查询对象
Mock<IGroupsRepository> groupRepo;
groupRepo.Setup(m => m.All).Returns(new List<GROUPS>()
{
new GROUPS { ID = 0, GROUP_NAME = "TEST1", CREATED_DATE = DateTime.Now, CREATED_BY = "Joe Blow", DESCRIPTION = "Test1" },
new GROUPS { ID = 1, GROUP_NAME = "TEST2", CREATED_DATE = DateTime.Now, CREATED_BY = "Joe Blow", DESCRIPTION = "Test2" }
});
返回一个可查询对象。
List<GROUPS>
不实现IQueryable<T>
,因此All
不可能返回这样的列表。
您可以调用列表上的AsQueryable
来获得列表的IQueryable<T>
表示。
var queryable = new List<GROUPS>()
{
new GROUPS { ID = 0, GROUP_NAME = "TEST1", CREATED_DATE = DateTime.Now, CREATED_BY = "Joe Blow", DESCRIPTION = "Test1" },
new GROUPS { ID = 1, GROUP_NAME = "TEST2", CREATED_DATE = DateTime.Now, CREATED_BY = "Joe Blow", DESCRIPTION = "Test2" }
}.AsQueryable();
groupRepo.Setup(m => m.All).Returns(queryable);
new List<GROUPS>()
{
new GROUPS { ID = 0, GROUP_NAME = "TEST1", CREATED_DATE = DateTime.Now, CREATED_BY = "Joe Blow", DESCRIPTION = "Test1" },
new GROUPS { ID = 1, GROUP_NAME = "TEST2", CREATED_DATE = DateTime.Now, CREATED_BY = "Joe Blow", DESCRIPTION = "Test2" }
}
.AsQueryable()
AsQueryable()
应使其与IQueryable
返回类型兼容