我遇到了一些关于使用 Moq 进行单元测试的参考资料。但是在查看 moq 的 wiki 示例后,我真的看不出比创建模拟存储库有什么好处。我错过了什么吗。下面是我创建的模拟存储库的示例。起订量可以以任何方式优化吗?
namespace Models
{
public class MockImagesRepository : IImagesRepository
{
private readonly BlogContext _context;
private readonly List<ImageViewModel> _imageViewModels;
public MockImagesRepository()
{
_context = new BlogContext();
_imageViewModels = new List<ImageViewModel>();
for (var i = 0; i < 10; i++)
{
_imageViewModels.Add(new ImageViewModel
{
Id = i,
AlternateText = "AlternateText " + i,
Description = "Description " + i,
Name = "Name " + i,
});
}
for (var i = 10; i < 20; i++)
{
_imageViewModels.Add(new ImageViewModel
{
Id = i,
AlternateText = "AlternateText " + i,
Description = "Description " + i,
Name = "Name " + i,
});
}
for (var i = 20; i < 30; i++)
{
_imageViewModels.Add(new ImageViewModel
{
Id = i,
AlternateText = "AlternateText " + i,
Description = "Description " + i,
Name = "Name " + i,
});
}
}
public int GetCount()
{
return _imageViewModels.Count;
}
public IEnumerable<ImageViewModel> FindRange(string format, int i, int recordsCount)
{
return _imageViewModels.Skip(i).Take(recordsCount);
}
public void Add(Image image)
{
}
public IEnumerable<Image> GetImages()
{
var images = new List<Image>();
foreach (var imageViewModel in _imageViewModels)
{
images.Add(new Image(imageViewModel));
}
return images;
}
#region Implementation of IDisposable
public void Dispose()
{
_context.Dispose();
}
#endregion
}
}