对于我的一个作业,我必须制作一个方法,该方法将使用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 - 永远不会从列表中删除。我已经解决了IEnumerable.Min
List
上的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
实现,因为它似乎永远不会返回Id为1的元素(通过在调试模式下查看List来确定)。 / p>
我错过了一些明显的东西吗?
注意: 我正在使用来自SharpRepository的模拟IRepository。并不是说它有所作为,因为Moq-ed接口没有固有的动作,但值得注意。
编辑:测试输出。
repo.Setup(rep => rep.Get(It.IsAny<int>())).Returns<int>(requests.ElementAt)
.Callback<int>(req => Console.WriteLine("Retrieved {0}", req));
EDIT2:所以我对Get()进行回调,如下所示:
Retrieved 0
Retrieved 1
Retrieved 1
现在我的输出看起来有点像这样:
GetNextRequest()
EDIT3:以下是 public ExportRequestRepository(IRepository<ExportRequest> exportRequestRepository)
{
_exportRequestRepository = exportRequestRepository;
}
中_exportRequestRepository的来源 - 在测试中它是模拟的存储库并被注入构造函数
{{1}}
答案 0 :(得分:0)
问题解决了。
问题是,当我应该使用ElementAt
时,我正在使用Find
,并且我在First()
之前调用GetNextRequest()
(获取下一个请求“弹出”元素来自底层存储库 - 可能会改变它。)
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