[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:SourceRealAppUnitTest_ToDoAppListTest.cs 97 Active
Repo is
public interface ITaskToDoRepository { Task<IEnumerable<TaskToDo>> GetTasks(); TaskToDo GetTasksById(Guid Id); }
I hope you could understand the problem. Thx
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0