简体   繁体   中英

cannot convert from 'IEnumerable' to 'IEnumerable

[TestMethod]
public async void  Select_getallTask()
{

    //Arrange
    IEnumerable<TaskToDo> list = new List<TaskToDo>();
                


    var mockrepo = new Mock<ITaskToDoRepository>();
      mockrepo.Setup(x => x.GetTasks()).Returns(list);

    //Act

    var data = mockrepo.Object.GetTasks();

    //Assert
    Assert.AreEqual(data, list);
}

this gives me error on

mockrepo.Setup(x => x.GetTasks()).Returns(list);

Error:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1503  Argument 1: cannot convert from
'System.Collections.Generic.IEnumerable<ToDoApp.Models.TaskToDo>' to
'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<ToDoApp.Models.TaskToDo>>'  UnitTest_ToDoApp    J:\Source\RealApp\UnitTest_ToDoApp\ListTest.cs  97  Active

Repo is

public interface ITaskToDoRepository
{
    Task<IEnumerable<TaskToDo>> GetTasks();
    TaskToDo GetTasksById(Guid Id);      

}

I hope you could understand the problem. Thx

You should use ReturnsAsync in your setup like as shown below

var mockrepo = new Mock<ITaskToDoRepository>();
                  mockrepo.Setup(x => x.GetTasks()).ReturnsAsync(list);

as your method GetTasks is type of Task<> return type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM