着从模拟我的仓库我的get()函数 - MongoDB.Driver 2.2.3 [英] Cant Mock my Get() function from my repository - MongoDB.Driver 2.2.3
问题描述
在我的单元测试我想测试我的方法,我从MongoDB的过滤数据创建
当我尝试嘲笑我的功能是这样的:
_repo.GetFluent<地点>((Arg.Any<表达式来; Func键<位置,布尔>>>()))
.Returns(X => locations.Where(x.Arg<表达式来; Func键<位置,布尔>>>())了ToList());
它强调了返回
说:
无法转换lambda表达式。
块引用>
在当我曾在我的简单的项目使用MongoDB的2.0.0驱动我没有问题,嘲笑像这样我的
的get()
的功能,但现在新的2.2.3驱动我有错误嘲笑他。有另一种方式?
的我见过的新的驱动程序使用
IFindFluent
和老一个我使用了MongoCursor
来得到我的数据。的
的我应该嘲笑
IFindFluent
不知何故?的
这是我对
GetFluent()的代码
法公共IFindFluent< TEntity,TEntity> GetFluent< TEntity>(System.Linq.Expressions.Expression< Func键< TEntity,布尔>>过滤器= NULL)其中TEntity:类,新的()
{
变种集合= GetCollection< TEntity>( );
如果(过滤器== NULL)
{
VAR emptyFilter =建设者< TEntity> .Filter.Empty;
返回collection.Find(emptyFilter);
}
,否则
{
VAR filterDefinition =建设者< TEntity> .Filter.Where(过滤器);
返回collection.Find(filterDefinition);
}
}
解决方案好从Usein Mambediev回答。有一个类似的例子,如何与 Typemock隔离嘲笑IFindFluent 不包成的接口:
[TestMethod的,孤立]
公共无效TestGet()
{
VAR的目标=新ClassUnderTest() ;
VAR returnMock = Isolate.Fake.Instance< IFindFluent<地点,地点>>();
INT大小= 3;
Isolate.WhenCalled(()=> returnMock.Count())WillReturn(大小)。
Isolate.WhenCalled(()=> target.GetFluent(默认值(表达式来; Func键<位置,布尔>>)))WillReturn(returnMock)。
Assert.AreEqual(大小,target.GetFluent<地点>(位置=>真).Count之间的());
}
我已经把你的方法纳入公共类只是为了测试一下。你只需要改变目标。
祝你好运!In my unit test I want to test my method that I created for filtering data from MongoDB.
When I try to mock my function like this:
_repo.GetFluent<Location>((Arg.Any<Expression<Func<Location, bool>>>())) .Returns(x => locations.Where(x.Arg<Expression<Func<Location, bool>>>()).ToList());
It underlines the
Returns
saying:Cannot convert lambda expression.
Before when I worked on my simple project using the 2.0.0 MongoDB driver I had no problem mocking my
Get()
function like this, but now with the new 2.2.3 driver I have an error mocking this. Is there another way?I've seen that the new driver is using
IFindFluent
and the older one I used theMongoCursor
to get my data.Should I mock the
IFindFluent
somehow?This is my code for the
GetFluent()
methodpublic IFindFluent<TEntity, TEntity> GetFluent<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>> filter = null) where TEntity : class, new() { var collection = GetCollection<TEntity>(); if (filter == null) { var emptyFilter = Builders<TEntity>.Filter.Empty; return collection.Find(emptyFilter); } else { var filterDefinition = Builders<TEntity>.Filter.Where(filter); return collection.Find(filterDefinition); } }
解决方案Good answer from Usein Mambediev. There is an similar example, how to mock IFindFluent with Typemock Isolator without wrapping it into the interface:
[TestMethod, Isolated] public void TestGet() { var target = new ClassUnderTest(); var returnMock = Isolate.Fake.Instance<IFindFluent<Location, Location>>(); int size = 3; Isolate.WhenCalled(() => returnMock.Count()).WillReturn(size); Isolate.WhenCalled(() => target.GetFluent(default(Expression<Func<Location, bool>>))).WillReturn(returnMock); Assert.AreEqual(size, target.GetFluent<Location>(location => true).Count()); }
I've put your method into the public class just in order to test. You only need to change the target. Good luck!
这篇关于着从模拟我的仓库我的get()函数 - MongoDB.Driver 2.2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!