gpt4 book ai didi

c# - 为什么我的模拟列表没有删除某个元素?

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:43 24 4
gpt4 key购买 nike

对于我的一项作业,我必须创建一个方法,该方法将使用 IRepository 作为基础从数据库返回下一个项目。我正在使用 TDD 设计周期,到目前为止,这就是我所拥有的:

    [TestMethod]
public void ShouldReturnNextExportRequest()
{
var requests = new List<ExportRequest>();

for (var i = 0; i < 100; i++)
{
var exportRequest = new Mock<ExportRequest>();
exportRequest.SetupGet(req => req.Id).Returns(i);

requests.Add(exportRequest.Object);
}

// Over-writing variables is bad
requests = requests.OrderBy(er => er.Id).ToList();

/**
* Mock the IRepository so that the Min() call returns the export request with the minimum Id
*/
var repo = new Mock<IRepository<ExportRequest>>();
repo.Setup(rep => rep.Min(It.IsAny<Expression<Func<ExportRequest, int>>>()))
.Returns<Expression<Func<ExportRequest, int>>>(expr => requests.Min(e => e.Id));

repo.Setup(rep => rep.Get(It.IsAny<int>())).Returns<int>(requests.ElementAt);
repo.Setup(rep => rep.Delete(It.IsAny<ExportRequest>()))
.Callback<ExportRequest>(req => Assert.IsTrue(requests.Remove(req), "Couldn't remove {0} from list", req.Id));


var exportRequestRepository = new ExportRequestRepository(repo.Object);
exportRequestRepository.GetNextRequest();
exportRequestRepository.GetNextRequest();

var third = exportRequestRepository.GetNextRequest();
var top = requests.First();
Assert.AreEqual(top.Id, third.Id, "Top element in list has id of {0}, but return from repository is {1}", top.Id, third.Id);
}

GetNextRequest() 方法只是执行以下操作:

    public ExportRequest GetNextRequest()
{
var top = _exportRequestRepository.Min(er => er.Id);
var element = _exportRequestRepository.Get(top);
_exportRequestRepository.Delete(element);
return element;
}

一切正常,但是第二个元素(id 为 1)永远不会从列表中删除。我已经解决了这个问题,归结为 List 上的 IEnumerable.Min 实现,因为它似乎永远不会返回 Id 为 1 的元素(通过查看确定 Debug模式下的列表)。

我是否漏掉了一些明显的东西?

注意:我正在使用来自 SharpRepository 的模拟 IRepository。这并不是说它有什么不同,因为 Moq-ed 界面没有固有的操作,但值得注意。

编辑:测试输出。

Test Name:  ShouldReturnNextExportRequest
Test FullName: ExportService.Tests.ExportRequestRepositorySpecification.ShouldReturnNextExportRequest
Test Source: c:\Users\dan.pantry\Desktop\RUMM\ExportService.Tests\ExportRequestRepositorySpecification.cs : line 17
Test Outcome: Failed
Test Duration: 0:00:00.211873

Result Message: Assert.AreEqual failed. Expected:<1>. Actual:<3>. Top element in list has id of 1, but return from repository is 3
Result StackTrace: at ExportService.Tests.ExportRequestRepositorySpecification.ShouldReturnNextExportRequest() in c:\Users\dan.pantry\Desktop\RUMM\ExportService.Tests\ExportRequestRepositorySpecification.cs:line 49

EDIT2:所以我像这样在 Get() 上设置了一个回调:

        repo.Setup(rep => rep.Get(It.IsAny<int>())).Returns<int>(requests.ElementAt)
.Callback<int>(req => Console.WriteLine("Retrieved {0}", req));

现在我的输出看起来有点像这样:

Retrieved 0
Retrieved 1
Retrieved 1

EDIT3:这是 GetNextRequest() 中 _exportRequestRepository 的来源 - 在测试中它是模拟的 Repository 并被注入(inject)到构造函数中

    public ExportRequestRepository(IRepository<ExportRequest> exportRequestRepository)
{
_exportRequestRepository = exportRequestRepository;
}

最佳答案

问题已解决。问题是我在应该使用 Find 时使用了 ElementAt,并且我在 GetNextRequest 之前调用了 First() ()(获取下一个请求“弹出”底层存储库中的元素 - 不过可能会改变它)。

        var repo = new Mock<IRepository<ExportRequest>>();
repo.Setup(rep => rep.Min(It.IsAny<Expression<Func<ExportRequest, int>>>()))
.Returns<Expression<Func<ExportRequest, int>>>(expr => requests.Min(e => e.Id));

repo.Setup(rep => rep.Get(It.IsAny<int>())).Returns<int>(i => requests.Find(er => er.Id == i));

repo.Setup(rep => rep.Delete(It.IsAny<ExportRequest>()))
.Callback<ExportRequest>(req => Assert.IsTrue(requests.Remove(req), "Couldn't remove {0} from list", req.Id));


var exportRequestRepository = new ExportRequestRepository(repo.Object);

Assert.AreEqual(0, exportRequestRepository.GetNextRequest().Id);
Assert.AreEqual(1, exportRequestRepository.GetNextRequest().Id);

var top = requests.First();
var third = exportRequestRepository.GetNextRequest();

Assert.AreEqual(top.Id, third.Id, "Top element in list has id of {0}, but return from repository is {1}", top.Id, third.Id);

解决的代码如上。如您所见,Get 现在使用 requests.Find() 而不是 requests.ElementAt

关于c# - 为什么我的模拟列表没有删除某个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21041055/

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