使用 IOC 进行 ASP.MVC 和 mvccontrib 流畅测试

发布于 2024-09-27 03:11:11 字数 1055 浏览 7 评论 0原文

我正在尝试使用流畅的测试助手来测试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺深海 2024-10-04 03:11:11

您可以编写一个允许您提供控制器实例的方法:

public static class GivenController
{
    public static ActionExpectations<T> As<T>(T controller) where T: Controller, new()
    {
        return new ActionExpectations<T> { MockController = controller };
    }
}

然后在单元测试中:

var controller = CreateMockedCustomerController();
GivenController
    .As<CustomerController>(controller)
    .ShouldRenderItself(RestfulAction.Index)
    .WhenCalling(x => x.Index());

要求是控制器应该是一个模拟对象,在我看来这不是一个非常干净的方法(测试模拟对象) 。您可以这样创建它:

var controller =  MockRepository
    .GeneratePartialMock<CustomerController>(new object[] { dep1, dep2 });

注意如何将依赖项传递给构造函数。

You could write a method which allows you to provide the instance of the controller:

public static class GivenController
{
    public static ActionExpectations<T> As<T>(T controller) where T: Controller, new()
    {
        return new ActionExpectations<T> { MockController = controller };
    }
}

And then in your unit test:

var controller = CreateMockedCustomerController();
GivenController
    .As<CustomerController>(controller)
    .ShouldRenderItself(RestfulAction.Index)
    .WhenCalling(x => x.Index());

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:

var controller =  MockRepository
    .GeneratePartialMock<CustomerController>(new object[] { dep1, dep2 });

Notice how dependencies are passed to the constructor.

国粹 2024-10-04 03:11:11

您是否尝试过属性注入而不是构造函数注入?

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

以歌曲疗慰 2024-10-04 03:11:11

我目前遇到了一个奇怪的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文