asp.net mvc3 UnitOfWork单元测试帮助 [英] asp.net mvc3 UnitOfWork unit test help
问题描述
我有一个工作单元,如下所示:
Hi,
I have a unit of work class which is as below:
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly EFDbContext context;
private IGenericRepository<Category> _categoryRepository;
private IGenericRepository<Product> _productRepository;
#region Contructor
public UnitOfWork()
{
context = new EFDbContext();
}
#endregion
public IGenericRepository<Category> CategoryRepository
{
get
{
if (_categoryRepository == null)
{
_categoryRepository = new EFGenericRepository<Category>(context);
}
return _categoryRepository;
}
}
public IGenericRepository<Product> ProductRepository
{
get
{
if (_productRepository == null)
{
_productRepository = new EFGenericRepository<Product>(context);
}
return _productRepository;
}
}
// Save.. Dispose etc omitted
}// end of class
我定义了一个新接口IPriceIncreaser.目的:提高产品库存中所有产品的价格
I defined a new Interface, IPriceIncreaser. Purpose: Increase prices of all products in Product inventory
public class PriceIncreaser : IPriceIncreaser
{
private IUnitOfWork unitOfWork;
public PriceIncreaser(IUnitOfWork unitOfWorkParam)
{
unitOfWork = unitOfWorkParam;
}
public void IncreasePrice(decimal increaseAmount)
{
foreach (Product p in unitOfWork.ProductRepository.Get())
{
p.Price += increaseAmount;
}
}
}
现在,我想使用Moq作为单元测试框架对仓库进行单元测试.
我编写了以下单元测试:
Now, i want to unit test my repo, using Moq as the unit testing framework.
I''ve written the following unit test:
[TestMethod]
public void Can_Increase_Prices()
{
var mockProductRepo = new Mock<IGenericRepository<Product>>();
mockProductRepo.Object.Insert(new Product { Name = "MP", Description ="HI", Price = 2500 });
// mock Unit of work here...
decimal initalProductTotalPrice = mockUnitOfWork.Object.ProductRepository.Get().Sum(p => p.Price);
decimal increaseAmount = 55;
// Act:
PriceIncreaser target = new PriceIncreaser(mockUnitOfWork.Object);
target.IncreasePrice(increaseAmount);
// Assert
Assert.AreNotEqual(initalProductTotalPrice, mockUnitOfWork.Object.ProductRepository.Get().Sum(p => p.Price));
}
但是运行测试时出现错误:
无法覆盖非虚拟成员... ProductRepository
通过阅读其他线程,我意识到Moq只会模拟接口和抽象方法/字段.
我想知道我现在如何编写模拟IProductric存储库的单元测试,该产品类型为IGenericRepository< Product>.包含在UnitOfWork.ProductRepository和UnitOfWork中的是一个IUnitOfWork.
任何和所有建议表示赞赏.
But i''m getting an error when i run the test which says:
Cannot override Non-Virtual member ...ProductRepository
I realize from reading other threads that Moq will only mock Interfaces, and abstract methods/fields.
I want to know how i can now write a unit test, which mocks my Product repository, which is of type IGenericRepository<Product> contained within UnitOfWork.ProductRepository and UnitOfWork is-a IUnitOfWork.
Any and all suggestions appreciated.
Thanks.
推荐答案
错误无法覆盖非虚拟成员... ProductRepository"表示Moq正在尝试覆盖"非虚拟方法.所以有定义的方法
The error "Cannot override Non-Virtual member ...ProductRepository" indicates that Moq is attempting "override" a method that is not virtual. So a method w/ definition
public void Something() { ... }
不能被覆盖.但这可以是:
cannot be overridden. But this can be :
public virtual void Something() { ... }
查看以下讨论使用Moq时重写虚拟方法的内容.
http://stackoverflow.com/questions/3539278/using-moq-to-override-virtual-methods-in-the-same-class [ ^ ]
Check out the following that discusses about overriding a virtual method when using Moq.
http://stackoverflow.com/questions/3539278/using-moq-to-override-virtual-methods-in-the-same-class[^]
这篇关于asp.net mvc3 UnitOfWork单元测试帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!