我是测试驱动开发的新手,并尝试对 mvc 应用程序进行单元测试。我正在使用 Moq 和 Ninject,并尝试遵循工作单元存储库模式。我的所有测试都收到 System.ArgumentException 错误。这是错误消息和错误堆栈跟踪:
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.
参数名称: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)
这是我的测试课:
[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);
}
我有一个 IUnitofWork 接口和类、通用存储库接口和类以及一个实现通用存储库的请求存储库
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);
}
使用 Ninject 我将 IUnitofwork 绑定到 UnitOfWork 类。
如果我将无参数构造函数添加到我的请求存储库,我的错误将得到解决 - 但因为我不想创建新的对象上下文,我想从工作单元存储库传递我的上下文。
如何解决此错误?