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() {
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) {
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?