使用 IOC 进行 ASP.MVC 和 mvccontrib 流畅测试
我正在尝试使用流畅的测试助手来测试 AbstractRestfulFluentController
public class CustomerController : AbstractRestfulFluentController
{
private readonly IService<Customer> _customerService;
private readonly IService<CustomerAddress> _addressService;
public CustomerController(IService<Customer> customerService, IService<CustomerAddress> addressService)
{
//Assume we use these in other actions
_customerService = customerService;
_addressService = addressService;
}
public ActionResult Index()
{
return View();
}
}
如您所见,我将一些服务注入控制器并使用 IOC 解析它们。我的问题是,如果没有无参数控制器,我发现使用 mvccontrib 中的流畅测试方法的所有示例都无法工作。
public void SuccessfulIndex()
{
GivenController.As<CustomerController>()
.ShouldRenderItself(RestfulAction.Index)
.WhenCalling(x => x.Index());
}
我不确定我需要做什么才能将 IOC 与 mvccontrib 中的流畅测试技术结合使用。我发现一些评论说这是可能的,但没有发现任何东西。为了实际使用 IOC 和 Fluent 测试,我该怎么做?
I am trying to use the fluent test helpers to test an AbstractRestfulFluentController
public class CustomerController : AbstractRestfulFluentController
{
private readonly IService<Customer> _customerService;
private readonly IService<CustomerAddress> _addressService;
public CustomerController(IService<Customer> customerService, IService<CustomerAddress> addressService)
{
//Assume we use these in other actions
_customerService = customerService;
_addressService = addressService;
}
public ActionResult Index()
{
return View();
}
}
As you can see I am injecting some services into the controller and resolving them using IOC. My problem is that all the examples I have found using the fluent test methods in mvccontrib don't work without a paramaterless controller.
public void SuccessfulIndex()
{
GivenController.As<CustomerController>()
.ShouldRenderItself(RestfulAction.Index)
.WhenCalling(x => x.Index());
}
I'm not sure what I need to do in order to be able to use IOC with the fluent test techniques in mvccontrib. I have found a few comments that it is possible but haven't found anything. What can I do in order to actually use IOC and fluent tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以编写一个允许您提供控制器实例的方法:
然后在单元测试中:
要求是控制器应该是一个模拟对象,在我看来这不是一个非常干净的方法(测试模拟对象) 。您可以这样创建它:
注意如何将依赖项传递给构造函数。
You could write a method which allows you to provide the instance of the controller:
And then in your unit test:
The requirement is that the controller should be a mock object which in my opinion is not a very clean approach (testing a mocked object). You could create it like this:
Notice how dependencies are passed to the constructor.
您是否尝试过属性注入而不是构造函数注入?
http://ninject.codeplex.com/wikipage?title=Injection%20Patterns
Have you tried property injection instead of constructor injection?
http://ninject.codeplex.com/wikipage?title=Injection%20Patterns
我目前遇到了一个奇怪的 ninject 问题,我刚刚发布了这个问题,但这至少应该为您指明正确的方向......所有代码都用于设置依赖项注入。
https://stackoverflow.com/questions/3909452/unit-testing- asp-net-mvc-controllers-with-ninject
I am currently having a weird ninject issue that I just posted about, but this should at least point you in the right direction...all the code is there for setting up the dependency injection.
https://stackoverflow.com/questions/3909452/unit-testing-asp-net-mvc-controllers-with-ninject