我可以在 ASP.NET MVC 控制器基类中测试 OnAuthorization 的重写吗?

发布于 2024-08-22 00:55:28 字数 828 浏览 3 评论 0原文

实现这项工作的机制并不难,但测试它有点奇怪。场景是,我想根据基本控制器(IPrincipal 对象)的 User 属性将一些基本用户数据转储到视图数据中,以便母版页始终拥有它。我需要访问我的 IUserManager (一个服务类),它由控制器工厂中的自定义 DI 提供。模拟用户对于测试来说是没有问题的。但是,对于基本控制器中的每个操作实现此目的的最简单方法是通过重写 OnAuthorization 方法来实现。基类看起来像这样:

public abstract class BaseController : Controller
{
    public BaseController(IUserManager userManager)
    {
        UserManager = userManager;
    }

    public IUserManager UserManager { get; private set; }

    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);
        UserManager.SetupUserViewData(User, ViewData);
    }
}

问题是我无法在测试中找到触发 OnAuth 方法的方法。我想在我的模拟 UserManager 中验证 SetupUserViewData 是否被调用。我没有使用自定义过滤器,因为我没有成熟的依赖注入框架(过滤器需要获取 IUserManager)。

有什么建议吗?由于这将在任何地方使用,我希望进行正确的测试。

The mechanics of making this work aren't hard, but testing it is a little strange. The scenario is that I want to dump some basic user data into the view data based on the User property of a base controller (an IPrincipal object) so that the master page always has it. I need access to my IUserManager (a service class), which is provided by custom DI in a controller factory. Mocking the user is no problem for testing. However, the easiest way to achieve this for every action in the base controller is to do it by overriding the OnAuthorization method. The base class then looks like this:

public abstract class BaseController : Controller
{
    public BaseController(IUserManager userManager)
    {
        UserManager = userManager;
    }

    public IUserManager UserManager { get; private set; }

    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);
        UserManager.SetupUserViewData(User, ViewData);
    }
}

The problem is that there's no way that I've been able to figure in a test to get the OnAuth method to fire. I'd like to verify in my mock UserManager that the SetupUserViewData gets called. I'm not using a custom filter because I don't have a full-blown dependency injection framework in place (the filter would need to get an IUserManager).

Any suggestions? As this will be used everywhere, I'd like to get the testing right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

-柠檬树下少年和吉他 2024-08-29 00:55:28

您可以通过创建一个从基本控制器继承的类,并使用调用 OnAuthorization 方法的公共方法来实现此目的。像这样的事情:

public class TestController : BaseController {
    public TestController(IUserManager userManager) : base(userManager) {
    }

    public void CallOnAuthorization(AuthorizationContext filterContext) {
        OnAuthorization(filterContext);
    }
}

然后你测试它像这样:

[Test]
public void TestMethod() {
    var userManager = //Mock usermanager;
    var filterContext = //Mock AuthorizationContext;
    var controller = new TestController(userManager);
    controller.CallOnAuthorization(filterContext);
    //Assert here
}

You can do this by creating a class that inherits from your base controller with a public method that calls your OnAuthorization method. Something like this:

public class TestController : BaseController {
    public TestController(IUserManager userManager) : base(userManager) {
    }

    public void CallOnAuthorization(AuthorizationContext filterContext) {
        OnAuthorization(filterContext);
    }
}

Then you test it something like this:

[Test]
public void TestMethod() {
    var userManager = //Mock usermanager;
    var filterContext = //Mock AuthorizationContext;
    var controller = new TestController(userManager);
    controller.CallOnAuthorization(filterContext);
    //Assert here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文