Reputation: 79
Ok i'm having trouble to form a good qeustion so please don't flame me on that what i'm trying to do in my code is moqing a list of which i get 1 object in the list. This object is retrieved by a .find method in the moqed database. It however doesn't return values because somehow in the method the data dissappears and my Person object is left with null i'll post some screenshots that will hopefully explain what i'm trying to say together with the testing method i'm using:
the method:
[TestMethod]
public void TestDetails()
{
Person test = new Person();
test.ID = 1;
test.City = "Eindhoven Area, Netherlands.";
test.Name = "Marijn van Donkelaar";
test.LearntSkillsAndLevelOfSkills = "Java:7, C#:4, SQL:4, PLSQL:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1";
test.ProfileImage = "/Images/hollemar.jpg";
test.PhoneNr = "0612345678";
test.SkillsToLearn = "ASP.net:2, JAVA:2";
test.Email = "[email protected]";
Person test1 = new Person();
test1.ID = 2;
test1.City = "Eindhoven Area, Netherlands.";
test1.Name = "Jan Pietjes";
test1.LearntSkillsAndLevelOfSkills = "Java:7, C#:4, SQL:4, PLSQL:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1";
test1.ProfileImage = "/Images/hollemar.jpg";
test1.PhoneNr = "0612345678";
test1.SkillsToLearn = "ASP.net:2, JAVA:2";
test1.Email = "[email protected]";
Person test2 = new Person();
test2.ID = 3;
test2.City = "Eindhoven Area, Netherlands.";
test2.Name = "Jasmine Test";
test2.LearntSkillsAndLevelOfSkills = "Java:7, C#:4, SQL:4, PLSQL:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1";
test2.ProfileImage = "/Images/hollemar.jpg";
test2.PhoneNr = "0612345678";
test2.SkillsToLearn = "ASP.net:2, JAVA:2";
test2.Email = "[email protected]";
var data = new List<Person> { test, test1, test2 }.AsQueryable();
var mockSet = new Mock<DbSet<Person>>();
mockSet.As<IQueryable<Person>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Person>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Person>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Person>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<PersonDBContext>();
mockContext.Setup(m => m.Persons).Returns(mockSet.Object);
var service = new PersonController(mockContext.Object);
ActionResult person = service.Details(1);
}
is there any particular reason for this? because i honestly don't see it. i figured it might be because the moq data only thinks it has to be called once and then dissappears but i have no idea... and i did check the data and saw that the ID that is given (1) is added to 1 of the objects and it's also visible in that passed on list
thanks in advance,
Marijn
Upvotes: 1
Views: 67
Reputation: 126052
You're using one IEnumerator
in your mocked object. This means that when your code is through enumerating over the items in it, it's at the end of the IEnumerator
, and subsequent evaluation of the enumerator will yield 0 results (since it's at the end).
You should be able to fix this just by returning a new IEnumerator
every time GetEnumerator
is called:
mockSet.As<IQueryable<Person>>().Setup(m => m.GetEnumerator())
.Returns(() => data.GetEnumerator());
Or, reuse the same enumerator, but reset it each time it's accessed:
var enumerator = data.GetEnumerator();
mockSet.As<IQueryable<Person>>().Setup(m => m.GetEnumerator())
.Returns(() =>
{
enumerator.Reset();
return enumerator;
});
Upvotes: 3