Click here to Skip to main content
15,892,161 members

asp.net mvc3 UnitOfWork unit test help

robroysd asked:

Open original thread
Hi,

I have a unit of work class which is as below:

C#
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


I defined a new Interface, IPriceIncreaser. Purpose: Increase prices of all products in Product inventory

C#
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;
            }
        }
    }


Now, i want to unit test my repo, using Moq as the unit testing framework.

I've written the following unit test:

C#
[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));
        }



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.
Tags: ASP.NET, MVC3, Unit Testing

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900