目前我正在读一本书(Pro ASP.Net Framework).
在本书中,作者建议使用Moq框架来帮助进行TDD.
[Test]
public void List_Presents_Correct_Page_Of_Products()
{
IProductsRepository repository = MockProductsRepository(
new Product { Name = "P1" }, new Product { Name = "P2" },
new Product { Name = "P3" }, new Product { Name = "P4" },
new Product { Name = "P5" }
);
ProductsController controller = new ProductsController(repository);
...
}
static IProductsRepository MockProductsRepository(params Product[] prods)
{
// Generate an implementor of IProductsRepository at runtime using Moq
var mockProductsRepos = new Moq.Mock<IProductsRepository>();
mockProductsRepos.Setup(x => x.Products).Returns(prods.AsQueryable());
return mockProductsRepos.Object;
}
Run Code Online (Sandbox Code Playgroud)
在模型层中,我们定义了一个FakeRepository和一个SqlRepository.
事实是我没有看到使用这个moq框架的优势.为什么我们不使用我们的FakeRepository?或者清除我们的FakeRepository并在其上添加假产品?
起初,我认为moq框架可以生成假数据,所以如果你有100个假对象可以生成,那么你不必这样做.
我想念的是什么?
R. *_*des 14
模拟框架优于手工模拟的一些优点:
一些缺点:
(这是我现在能想到的.随意编辑并添加更多内容.)
| 归档时间: |
|
| 查看次数: |
2236 次 |
| 最近记录: |