TDD Unit of Work Pattern Moq Unable to create proxy class error code

I am new to test development and am trying to use the mvc unit test application. I am using Moq and Ninject and trying to follow the work unit repository pattern. I get a System.ArgumentException error for all my tests. Here is the error message and the error stack trace:

Test method LOMSv4.Tests.Controllers.AutobytelControllerTest.Index_Contains_All_Requests threw exception: System.ArgumentException: Can not instantiate proxy of class: LOMSv4_DAL.Autobytel.Concrete.RequestRepository. Could not find a parameterless constructor. 

Parameter Name: constructorArguments

 Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments) Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors) Moq.Proxy.CastleProxyFactory.CreateProxy[T](ICallInterceptor interceptor, Type[] interfaces, Object[] arguments) Moq.Mock`1.<InitializeInstance>b__0() Moq.PexProtector.Invoke(Action action) Moq.Mock`1.InitializeInstance() Moq.Mock`1.OnGetObject() Moq.Mock.GetObject() Moq.Mock.get_Object() Moq.MockDefaultValueProvider.ProvideDefault(MethodInfo member) Moq.QueryableMockExtensions.FluentMock[T,TResult](Mock`1 mock, Expression`1 setup) lambda_method(Closure ) Moq.Mock.GetInterceptor(Expression fluentExpression, Mock mock) Moq.Mock.<>c__DisplayClass1c`2.<Setup>b__1b() Moq.PexProtector.Invoke[T](Func`1 function) Moq.Mock.Setup[T,TResult](Mock mock, Expression`1 expression, Func`1 condition) Moq.Mock`1.Setup[TResult](Expression`1 expression) 

Here is my test class:

 [TestClass] public class AutobytelControllerTest { Mock<IUnitOfWork> mock = new Mock<IUnitOfWork>(); [TestMethod] public void Index_Contains_All_Requests() { //Arrange AutobytelController controller = new AutobytelController(mock.Object); mock.Setup(m => m.RequestRepository.SelectAll()).Returns(new abtRequest[] { new abtRequest {RequestID = 1, Title = "Request 1", Description = "Request Description1", UserName = "NewUser", RequestStatus = 0}, new abtRequest {RequestID = 2, Title = "Request 2", Description = "Request Description2", UserName = "ReturnUser", RequestStatus = 1} }.AsQueryable()); //Act abtRequest[] result = ((AutobytelHomeViewModel)controller.Index().Model).Requests.ToArray(); //Assert Assert.AreEqual(result.Length, 2); Assert.AreEqual("Request 1", result[0].Title); Assert.AreEqual("Request 2", result[1].Title); } 

I have an IUnitofWork interface and a class, a common repository interface, and a class and request repository that implements a common repository

 public interface IUnitOfWork { RequestRepository RequestRepository { get; } void Save(); } 

 public class UnitOfWorkRepository : IUnitOfWork, IDisposable { private AutobytelEntities context = new AutobytelEntities(); private RequestRepository requestRepository; public RequestRepository RequestRepository { get { if (this.requestRepository == null) { //this.requestRepository = new GenericRepository<abtRequest>(context); this.requestRepository = new RequestRepository(context); } return requestRepository; } } public void Save() { context.SaveChanges(); } private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { context.Dispose(); } } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } } 

 public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class { internal AutobytelEntities context; internal IObjectSet<TEntity> objectSet; public GenericRepository(AutobytelEntities context) { this.context = context; this.objectSet = context.CreateObjectSet<TEntity>(); } public virtual IQueryable<TEntity> SelectAll() { return objectSet.AsQueryable(); } public virtual TEntity Update(TEntity entity) { context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); return entity; } public virtual void Delete(TEntity entity) { objectSet.DeleteObject(entity); } } 

  public class RequestRepository : GenericRepository<abtRequest>, IGenericRepository<abtRequest> { public RequestRepository(AutobytelEntities context) : base(context) { } public virtual abtRequest SelectByUserName(string username) { return context.abtRequests.FirstOrDefault(i => i.UserName == username && (i.RequestStatus == 0 || i.RequestStatus == 1)); } public virtual abtRequest SelectByRequestID(int requestID) { return context.abtRequests.FirstOrDefault(i => i.RequestID == requestID); } 

Using Ninject, I bind my IUnitofwork to the UnitOfWork class.

If I add a parameterless constructor to my query repository, my error will be resolved, but since I do not want to create a new object context, I want to transfer my context from the unit of work repository.

How can I solve this error?

+4
source share
2 answers

I solved this error by adding an interface to my RequestRepository and creating an instance of this interface in my part of the work. My IUitit of Work has changed to:

 public interface IUnitOfWork { IRequestRepository RequestRepository { get; } void Save(); } 

Inside my repository UnitOfWork changed to:

 private IRequestRepository requestRepository; public IRequestRepository RequestRepository { get { if (this.requestRepository == null) { this.requestRepository = new RequestRepository(context); } return requestRepository; } } 
+6
source

This is just pseudo code, but I hope it brings you closer to the solution. You should be able to customize your layouts on these lines:

 //Arrange var mockRepository = new Mock<RequestRepository>("CONSTRUCTOR ARGUMENT"); mockRepository.Setup(mr => mr.SelectAll()).Returns(new abtRequest[] { new abtRequest {RequestID = 1, Title = "Request 1", Description = "Request Description1", UserName = "NewUser", RequestStatus = 0}, new abtRequest {RequestID = 2, Title = "Request 2", Description = "Request Description2", UserName = "ReturnUser", RequestStatus = 1} }.AsQueryable()); mock.SetupGet(uow => uow.RequestRepository).Returns(mockRepository.Object); AutobytelController controller = new AutobytelController(mock.Object); 

The code above has a placeholder for your constructor argument ("CONSTRUCTOR ARGUMENT") - just put the simplest thing here that will work for your test case.

+2
source

All Articles