如何Moq IFindFluent,以便此对ToListAsync的调用有效? [英] How do I Moq IFindFluent so this call to ToListAsync works?
问题描述
我正在对MongoDB C#驱动程序的包装器进行单元测试.我有这行代码:
I am unit testing a wrapper to the MongoDB C# driver. I have this line of code:
Collection.Find(predicate).ToListAsync();
其中Collection
是IMongoCollection<T>
类型,而Find(predicate)
返回实现IFindFluent<T, T>
的实例.我认为ToListAsync()
是将结果转换为列表的扩展.
Where Collection
is of type IMongoCollection<T>
and Find(predicate)
returns an instance implementing IFindFluent<T, T>
. ToListAsync()
is an extension to turn the results into a list, I assume.
我正在尝试编写单元测试,但在处理此问题上我陷入了困境.我无法制作包装器类,因为这就是我正在研究的内容.我更愿意这样做,以便ToListAsync()
返回创建的列表,或者模拟Find()
以返回可以作为列表的内容.
I am attempting to write unit tests, and I am stumped on handling this. I can't make a wrapper class because that's what I'm working on. I would prefer to either make it so ToListAsync()
returns a created list OR to mock the Find()
to return something that can be made a list of.
推荐答案
我为此专门制作了一个抽象层,因为我找不到合适的东西并且寻求帮助没有结果.
I wound up making a little abstraction layer for this, since I couldn't find anything suitable and asking for help yielded no answers.
我创建了一个名为AppCollection
的接口/实现对,专门用于处理MongoDB接口. IAppCollection
将具有用于IAppCollection.ToList(predicate)
的方法,而AppCollection
将运行Collection.Find(predicate).ToListAsync();
调用,并返回列表.后来,模拟IAppCollection
以确保进行正确的调用是一件微不足道的事情.虽然我无法在本地LINQ中测试谓词,但我至少可以编译谓词并将其与通过/失败对象进行比较.
I created an interface/implementation pair called AppCollection
specifically to handle the MongoDB interface. IAppCollection
would have a method for IAppCollection.ToList(predicate)
, and the AppCollection
would run the Collection.Find(predicate).ToListAsync();
call, returning the list. Later, it was a trivial matter to mock the IAppCollection
to make sure the right calls were being made. While I couldn't test the predicates in native LINQ, I could at least compile the predicates and compare them to passing/failing objects.
这篇关于如何Moq IFindFluent,以便此对ToListAsync的调用有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!