相关疑难解决方法(0)

在ASP.NET Core中模拟IPrincipal

我有一个ASP.NET MVC核心应用程序,我正在编写单元测试.其中一个操作方法使用用户名来实现某些功能:

SettingsViewModel svm = _context.MySettings(User.Identity.Name);
Run Code Online (Sandbox Code Playgroud)

这显然在单元测试中失败了.我环顾四周,所有建议都是从.NET 4.5到模拟HttpContext.我相信有更好的方法可以做到这一点.我试图注入IPrincipal,但它引发了一个错误; 我甚至试过这个(出于绝望,我想):

public IActionResult Index(IPrincipal principal = null) {
    IPrincipal user = principal ?? User;
    SettingsViewModel svm = _context.MySettings(user.Identity.Name);
    return View(svm);
}
Run Code Online (Sandbox Code Playgroud)

但这也引发了一个错误.无法在文档中找到任何内容......

c# unit-testing xunit.net asp.net-core-mvc

73
推荐指数
3
解决办法
1万
查看次数

标签 统计

asp.net-core-mvc ×1

c# ×1

unit-testing ×1

xunit.net ×1