如何模拟返回Task< IEnumerable< T>>的方法与Task< List< T>>吗? [英] How to mock a method returning Task<IEnumerable<T>> with Task<List<T>>?
问题描述
我正在尝试在模拟接口方法的地方设置单元测试初始化程序(在Moq中):
public interface IRepository
{
Task<IEnumerable<myCustomObject>> GetSomethingAsync(string someStringParam);
}
...
[TestInitialize]
public void Initialize()
{
var repoMock = new Mock<IRepository>();
var objectsList = new List<myCustomObject>() {
new myCustomObject("Something"),
new myCustomObject("Otherthing")
}
repoMock.Setup<Task<IEnumerable<myCustomObject>>>(
rep => rep.GetSomethingAsync("ThisParam")
).Returns(Task.FromResult(objectsList)); //error here
}
我遇到的问题是我不知道如何获取返回我的objectsList
的方法.实际上,这当然可以正常工作,因为List实现了IEnumerable,但是当以Moq封装在Task中时,它似乎不起作用.我得到的错误是在Returns()
方法上:
Argument 1: cannot convert from 'System.Threading.Tasks.Task<
System.Collections.Generic.List<myCustomObject>>'
to 'System.Threading.Tasks.Task<
System.Collections.Generic.IEnumerable<myCustomObject>>'
作为一种解决方法,我可以创建另一个方法,该方法仅创建List<myObj>
并将其返回为IEnumerable<myObj>
,但是我觉得必须有另一种方法来处理此问题. /p>
那么,如何在不创建帮助器方法的情况下执行此操作?
您需要在Task.FromResult
上指定<IEnumerable<myCustomObject>>
,如下所示:
[TestInitialize]
public void Initialize()
{
var repoMock = new Mock<IRepository>();
var objectsList = new List<myCustomObject>() {
new myCustomObject("Something"),
new myCustomObject("Otherthing")
}
repoMock.Setup<Task<IEnumerable<myCustomObject>>>(
rep => rep.GetSomethingAsync("ThisParam")
).Returns(Task.FromResult<IEnumerable<myCustomObject>>(objectsList));
}
或者,您可以在从List<myCustomObject>
分配objectList
之前将其声明为<IEnumerable<myCustomObject>>
.不需要先前的建议.
I'm attempting to set up a unit test initializer (in Moq) where an interface method is being mocked:
public interface IRepository
{
Task<IEnumerable<myCustomObject>> GetSomethingAsync(string someStringParam);
}
...
[TestInitialize]
public void Initialize()
{
var repoMock = new Mock<IRepository>();
var objectsList = new List<myCustomObject>() {
new myCustomObject("Something"),
new myCustomObject("Otherthing")
}
repoMock.Setup<Task<IEnumerable<myCustomObject>>>(
rep => rep.GetSomethingAsync("ThisParam")
).Returns(Task.FromResult(objectsList)); //error here
}
The issue I'm having is I can't figure out how to get the method to return my objectsList
. In practice this of course works fine because List implements IEnumerable, but it doesn't seem to work when wrapped in a Task, in Moq. The error I get is on the Returns()
method:
Argument 1: cannot convert from 'System.Threading.Tasks.Task<
System.Collections.Generic.List<myCustomObject>>'
to 'System.Threading.Tasks.Task<
System.Collections.Generic.IEnumerable<myCustomObject>>'
As a workaround I can create another method that simply creates the List<myObj>
and returns it as IEnumerable<myObj>
, but I feel like there must be another way to handle this that is cleaner.
So, how would I do this without having to create the helper method?
You need to specify the <IEnumerable<myCustomObject>>
on the Task.FromResult
, like this:
[TestInitialize]
public void Initialize()
{
var repoMock = new Mock<IRepository>();
var objectsList = new List<myCustomObject>() {
new myCustomObject("Something"),
new myCustomObject("Otherthing")
}
repoMock.Setup<Task<IEnumerable<myCustomObject>>>(
rep => rep.GetSomethingAsync("ThisParam")
).Returns(Task.FromResult<IEnumerable<myCustomObject>>(objectsList));
}
Alternatively you could declare your objectList
as <IEnumerable<myCustomObject>>
before assigning it from the List<myCustomObject>
. Making the previous suggestion not required.
这篇关于如何模拟返回Task< IEnumerable< T>>的方法与Task< List< T>>吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!