将最小起订量与温莎城堡一起使用
Using Moq With Castle Windsor
我正在尝试使用最小起订量对我的家庭控制器进行简单的单元测试,但出现异常
'Castle.MicroKernel.ComponentNotFoundException' occurred in Castle.Windsor.dll but was not handled in user code. No component for supporting the service OrderTrackingSystem.Core.Repositories.IRepository was found
在我的 HomeController 上 _repository = MvcApplication.Container.Resolve<IRepository>()
。
private IRepository _repository;
_repository = MvcApplication.Container.Resolve<IRepository>();
public HomeController(IRepository repository)
{
_repository = repository;
}
这是我的单元测试代码。
[TestClass]
public class HomeControllerTests
{
private Mock<IRepository> _repositoryMock;
[TestInitialize]
public void TestSetup()
{
_repositoryMock = new Mock<IRepository>();
}
[TestMethod]
public void HomeControllerIndexReturnsAView()
{
// Arrange
var controller = new HomeController(_repositoryMock.Object);
// Act
var result = controller.Index() as ViewResult;
// Assert
Assert.IsNotNull(result);
}
}
我觉得在我的单元测试中注册或设置存储库时一定缺少一些简单的东西。有什么想法吗?
我没有使用过 Castle Windsor,但查看您的控制器,它似乎使用构造函数注入来提供控制器依赖项。这 tutorial 表明探测 Windsor 的通常方法是创建一个从 DefaultControllerFactory 派生的 ControllerFactory,它在内部使用内核来解析控制器的依赖关系。这似乎证实了您代码中的这一行是多余的:
_repository = MvcApplication.Container.Resolve<IRepository>();
所以,您的代码应该是:
private IRepository _repository;
// The repository is being created + injected by Windsor.
// If this wasn't correctly wired up already, you'd be getting errors
// indicating that your HomeController didn't have a default constructor...
public HomeController(IRepository repository)
{
_repository = repository;
}
删除对 Resolve 的调用后,您应该能够从单元测试中将模拟依赖项注入 class,如您所述。
虽然在某些情况下您可能需要从控制器工厂外部访问 Windsor 内核来解决依赖关系,但这种情况应该很少见。
我正在尝试使用最小起订量对我的家庭控制器进行简单的单元测试,但出现异常
'Castle.MicroKernel.ComponentNotFoundException' occurred in Castle.Windsor.dll but was not handled in user code. No component for supporting the service OrderTrackingSystem.Core.Repositories.IRepository was found
在我的 HomeController 上 _repository = MvcApplication.Container.Resolve<IRepository>()
。
private IRepository _repository;
_repository = MvcApplication.Container.Resolve<IRepository>();
public HomeController(IRepository repository)
{
_repository = repository;
}
这是我的单元测试代码。
[TestClass]
public class HomeControllerTests
{
private Mock<IRepository> _repositoryMock;
[TestInitialize]
public void TestSetup()
{
_repositoryMock = new Mock<IRepository>();
}
[TestMethod]
public void HomeControllerIndexReturnsAView()
{
// Arrange
var controller = new HomeController(_repositoryMock.Object);
// Act
var result = controller.Index() as ViewResult;
// Assert
Assert.IsNotNull(result);
}
}
我觉得在我的单元测试中注册或设置存储库时一定缺少一些简单的东西。有什么想法吗?
我没有使用过 Castle Windsor,但查看您的控制器,它似乎使用构造函数注入来提供控制器依赖项。这 tutorial 表明探测 Windsor 的通常方法是创建一个从 DefaultControllerFactory 派生的 ControllerFactory,它在内部使用内核来解析控制器的依赖关系。这似乎证实了您代码中的这一行是多余的:
_repository = MvcApplication.Container.Resolve<IRepository>();
所以,您的代码应该是:
private IRepository _repository;
// The repository is being created + injected by Windsor.
// If this wasn't correctly wired up already, you'd be getting errors
// indicating that your HomeController didn't have a default constructor...
public HomeController(IRepository repository)
{
_repository = repository;
}
删除对 Resolve 的调用后,您应该能够从单元测试中将模拟依赖项注入 class,如您所述。
虽然在某些情况下您可能需要从控制器工厂外部访问 Windsor 内核来解决依赖关系,但这种情况应该很少见。