gpt4 book ai didi

c# - 使用 Moq 安排单元测试

转载 作者:行者123 更新时间:2023-11-30 20:22:29 27 4
gpt4 key购买 nike

我创建了一个名为 ListOfPeople 的方法,并尝试为其编写一个单元测试。目前,我在设置单元测试Arrange 部分时遇到问题。我现在无法运行它,因为我的 PeopleIdPersonGroupJobId 状态为 Cannot resolve symbol

Question: How can I test this method successfully?

我的方法:

public IEnumerable<Person> ListOfPeople(int personId, int? jobId)
{
var people = _unitOfWork.GetRepository<DB.Person>().GetAll()
.Where(p => p.PeopleGroups.Any(m => m.PeopleId == personId
&& m.PersonGroupdJobId == jobId));

if (!people.Any())
return new List<Person>();

var personData = people.Select(p => new Person
{
Id = p.PersonId,
Name = p.PersonName,
WorkTypeId = p.WorkTypeId,
PrimaryLanguage = p.PrimaryLanguage,
PeopleGroups = p.PeopleGroups.ToList()
.Select(pp => new PeopleGroup
{
Id = pp.ProjectPartyId,
PeopleId = pp.PeopleId,
JobId = pp.PersonGroupJobId ?? 0,
AddressId = pp.AddressId
})
}).ToList();

return personData;
}

单元测试:

[TestMethod]
public void ListOfPeople_peopleExist_returnsList()
{
// Arrange
var people = new List<DB.Person>
{
PeopleId = It.IsAny<int>(),
PersonGroupdJobId = It.IsAny<int>()
};

_uowMock
.Setup(mock => mock.GetRepository<DB.Person>().GetAll())
.Returns(people.AsQueryable());

// Act
var result = _repository.ListOfPeople(It.IsAny<int>(), It.IsAny<int>());

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(2, result.Count());

_unitOfWork.Verify(mock => mock.Commit(), Times.Never());
_unitOfWork.Verify(mock => mock.GetRepository<DB.Person>().GetAll(), Times.Once());
}

最佳答案

您没有使用 Moq's It匹配器正确。匹配器旨在用于最小起订量的 SetupVerify调用(例如 see here 的用法)

  • 在安排中,你不应该使用 It.IsAny在创建虚假数据时。 It.IsAny<> 将会发生的一切是这些值将被分配 default(Type) ,即整数等为零,这不会有用。

var people = new List<DB.Person>
{
new DB.Person
{
PeopleId = 1234, // Some traceable value and NOT It.IsAny<int>(),
PersonGroupdJobId = 987,
...
  • 在 act 步骤中,您需要使用执行场景的值调用被测方法(同样,不仅仅是 It.IsAny<> ):

var result = _repository.ListOfPeople(1234, 567); // Filters matching your test scenario

此外,由于被测试的方法至少有两个问题,即对存储库应用过滤器,然后将一个 DB 实体映射到另一个 POCO,您需要提供一组完整的输入数据,以便您可以证明所有输入字段都映射到输出字段。

如果将所有这些放在一起,您的单元测试将类似于:

[Test]
public void ListOfPeople_peopleExist_returnsList()
{
// Arrange : Generate fake data
var people = new List<DB.Person>
{
new DB.Person
{
PersonId = 123,
PersonName = "Bob",
PrimaryLanguage = "French",
WorkTypeId = 987,
PeopleGroups = new []
{
new DB.PeopleGroup
{
AddressId = 123,
PersonGroupJobId = 999,
PeopleId = 123, // Match the parent ID
ProjectPartyId = 3
}
}
}
};

// Your _unitOfWork and _repository mocks seem to be class private fields
var _unitOfWork = new Mock<IUnitOfWork>();

_unitOfWork
.Setup(mock => mock.GetRepository<DB.Person>().GetAll())
.Returns(people.AsQueryable());

var _repository = new MyRepo(_unitOfWork.Object);

// Act
var result = _repository.ListOfPeople(123, 999);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count(r => r.Name == "Bob"
&& r.Id == 123
&& r.PeopleGroups.First().Id == 3));
//... etc - ensure all the fields are mapped

_unitOfWork.Verify(mock => mock.Commit(), Times.Never());
_unitOfWork.Verify(mock => mock.GetRepository<DB.Person>().GetAll(),
Times.Once());
}

关于c# - 使用 Moq 安排单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31432943/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com