mockset 的多个设置 ASP.net 仅执行 1 个 mock
Multiple setups for a mockset ASP.net only carries out 1 mock
所以我的问题是我必须模拟两件事。一个是找到一个人的方法,另一个是它迭代的那些人的列表。然而,它并没有得到两个设置,而只有一个。我试过将它们按不同的顺序排列,每次只有前 1 个有效。
这是我的代码:
Person test = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 1,
ID = 1,
Email = "fraylight@gmail.com",
ExtraInfo = "blabla",
HobbyProjectICTRelated = "a",
Hobbys = "",
LearntSkillsAndLevelOfSkills = "Java:7, C#:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1",
Name = "Marijn van Donkelaar",
PhoneNr = "0612345678",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:3",
Stand = "",
Summary = "",
YearsOfWorkExperience = 6,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
Person test1 = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 2,
ID = 2,
Email = "fraylight@gmail.com",
ExtraInfo = "",
HobbyProjectICTRelated = "a",
Hobbys = "zwemmen",
LearntSkillsAndLevelOfSkills = "Java:8, C#:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1",
Name = "Richard Holleman",
PhoneNr = "",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:2",
Stand = "",
Summary = "",
YearsOfWorkExperience = 16,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
Person test2 = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 3,
ID = 3,
Email = "fraylight@gmail.com",
ExtraInfo = "",
HobbyProjectICTRelated = "",
Hobbys = "zwemmen",
LearntSkillsAndLevelOfSkills = "C#:4, SQL:4, PLSQL:4, HTML:2, CSS:2, jQuery:1",
Name = "Jasmine Test",
PhoneNr = "0612345678",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:1",
Stand = "",
Summary = "",
YearsOfWorkExperience = 11,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
var data = new List<Person> { test, test1, test2 }.AsQueryable();
var dbSetMock = new Mock<IDbSet<Person>>();
dbSetMock.Setup(m => m.Provider).Returns(data.Provider);
dbSetMock.Setup(m => m.Expression).Returns(data.Expression);
dbSetMock.Setup(m => m.ElementType).Returns(data.ElementType);
dbSetMock.Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
var mockContext = new Mock<PersonDBContext>();
mockContext.Setup(x => x.Persons).Returns(dbSetMock.Object);
mockContext.Setup(x => x.Persons.Find(1)).Returns(test);
var service = new PersonController(mockContext.Object);
var controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(t => t.HttpContext.Session["loggedinuser"]).Returns(10);
service.ControllerContext = controllerContext.Object;
ViewResult detailspageresultcorrect = (ViewResult) service.Details(10);
Person resultpersoncorrect = (Person) detailspageresultcorrect.Model;
Assert.IsTrue(resultpersoncorrect.Name.Equals(test.Name));
出错的地方是:var mockContext = new Mock();
您应该能够模拟针对 IDbSet
的 Find
方法,而不是通过 Persons 属性.
因此您的设置如下所示:
var dbSetMock = new Mock<IDbSet<Person>>();
dbSetMock.Setup(m => m.Provider).Returns(data.Provider);
dbSetMock.Setup(m => m.Expression).Returns(data.Expression);
dbSetMock.Setup(m => m.ElementType).Returns(data.ElementType);
dbSetMock.Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
dbSetMock.Setup(m => m.Find(1)).Returns(test);
var mockContext = new Mock<PersonDBContext>();
mockContext.Setup(x => x.Persons).Returns(dbSetMock.Object);
所以我的问题是我必须模拟两件事。一个是找到一个人的方法,另一个是它迭代的那些人的列表。然而,它并没有得到两个设置,而只有一个。我试过将它们按不同的顺序排列,每次只有前 1 个有效。
这是我的代码:
Person test = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 1,
ID = 1,
Email = "fraylight@gmail.com",
ExtraInfo = "blabla",
HobbyProjectICTRelated = "a",
Hobbys = "",
LearntSkillsAndLevelOfSkills = "Java:7, C#:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1",
Name = "Marijn van Donkelaar",
PhoneNr = "0612345678",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:3",
Stand = "",
Summary = "",
YearsOfWorkExperience = 6,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
Person test1 = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 2,
ID = 2,
Email = "fraylight@gmail.com",
ExtraInfo = "",
HobbyProjectICTRelated = "a",
Hobbys = "zwemmen",
LearntSkillsAndLevelOfSkills = "Java:8, C#:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1",
Name = "Richard Holleman",
PhoneNr = "",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:2",
Stand = "",
Summary = "",
YearsOfWorkExperience = 16,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
Person test2 = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 3,
ID = 3,
Email = "fraylight@gmail.com",
ExtraInfo = "",
HobbyProjectICTRelated = "",
Hobbys = "zwemmen",
LearntSkillsAndLevelOfSkills = "C#:4, SQL:4, PLSQL:4, HTML:2, CSS:2, jQuery:1",
Name = "Jasmine Test",
PhoneNr = "0612345678",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:1",
Stand = "",
Summary = "",
YearsOfWorkExperience = 11,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
var data = new List<Person> { test, test1, test2 }.AsQueryable();
var dbSetMock = new Mock<IDbSet<Person>>();
dbSetMock.Setup(m => m.Provider).Returns(data.Provider);
dbSetMock.Setup(m => m.Expression).Returns(data.Expression);
dbSetMock.Setup(m => m.ElementType).Returns(data.ElementType);
dbSetMock.Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
var mockContext = new Mock<PersonDBContext>();
mockContext.Setup(x => x.Persons).Returns(dbSetMock.Object);
mockContext.Setup(x => x.Persons.Find(1)).Returns(test);
var service = new PersonController(mockContext.Object);
var controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(t => t.HttpContext.Session["loggedinuser"]).Returns(10);
service.ControllerContext = controllerContext.Object;
ViewResult detailspageresultcorrect = (ViewResult) service.Details(10);
Person resultpersoncorrect = (Person) detailspageresultcorrect.Model;
Assert.IsTrue(resultpersoncorrect.Name.Equals(test.Name));
出错的地方是:var mockContext = new Mock();
您应该能够模拟针对 IDbSet
的 Find
方法,而不是通过 Persons 属性.
因此您的设置如下所示:
var dbSetMock = new Mock<IDbSet<Person>>();
dbSetMock.Setup(m => m.Provider).Returns(data.Provider);
dbSetMock.Setup(m => m.Expression).Returns(data.Expression);
dbSetMock.Setup(m => m.ElementType).Returns(data.ElementType);
dbSetMock.Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
dbSetMock.Setup(m => m.Find(1)).Returns(test);
var mockContext = new Mock<PersonDBContext>();
mockContext.Setup(x => x.Persons).Returns(dbSetMock.Object);