我收到了错误消息。我正在使用Moq和Ninject。以下是详细的错误消息:
[MissingMethodException:没有为此定义的无参数构造函数 对象] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型, Boolean publicOnly,Boolean noCheck,Boolean& canBeCached, RuntimeMethodHandleInternal&安培; ctor,布尔& bNeedSecurityCheck)+0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark& stackMark)+113
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis,Boolean fillCache,StackCrawlMark& stackMark) +232 System.Activator.CreateInstance(Type type,Boolean nonPublic)+83 System.Activator.CreateInstance(Type type)+66 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext,Type controllerType)+55[InvalidOperationException:尝试创建时出错 'EF.Controllers.ProductController'类型的控制器。确保这一点 控制器有一个无参数的公共构造函数。]
System.Web.Mvc.DefaultControllerActivator.Create(的RequestContext requestContext,Type controllerType)+179
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(的RequestContext requestContext,Type controllerType)+80
System.Web.Mvc.DefaultControllerFactory.CreateController(的RequestContext requestContext,String controllerName)+74
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController&控制器,IControllerFactory&厂) +193 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+49
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback回调,对象状态)+49
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext的 context,AsyncCallback cb,Object extraData)+16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously)+155
这是我的控制器:
public class ProductController : Controller
{
private IProductRepository repository;
public ProductController(IProductRepository productRepository)
{
this.repository = productRepository;
}
public ViewResult List()
{
return View(repository.Products);
}
}
RouteConfig 指出 List ActionMethod。以下是 IProductRepository 的内容:
public interface IProductRepository { IQueryable<Product> Products { get; } }
以下是产品的定义:
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
这是 NinjectControllerFactory 类:
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
/// <summary>
/// An binding has been implemented there. I am creating a Mock object, and I embedded it.
/// </summary>
private void AddBindings()
{
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new List<Product> {
new Product { Name = "Football", Price = 25 },
new Product { Name = "Surf board", Price = 179 },
new Product { Name = "Running shoes", Price = 95 }
}.AsQueryable());
ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
}
}
我缺少什么?请你帮助我好吗?提前致谢。
答案 0 :(得分:0)
您对Moq的使用不正确。您没有使用Moq进行依赖注入,在单元测试另一个依赖于该接口的类时,您可以使用它来模拟接口(理想情况下)。你甚至不应该在NinjectControllerFactory中使用Moq。
您的AddBindings()
方法应该只是:
ninjectKernel.Bind<IProductRepository>().To<ProductRepository>();
此外,通常DI容器默认采用约定方法,这使您甚至不需要在那里使用Bind<>().To<>()
语句。我不熟悉Ninject,但我会尝试为你找一个样本。