gpt4 book ai didi

asp.net-mvc - 在 Controller 上使用依赖注入(inject)进行单元测试

转载 作者:行者123 更新时间:2023-12-05 03:06:43 26 4
gpt4 key购买 nike

我厌倦了需要填写字段然后登录注销来做一个简单的测试。

我搜索了 xUnit 和 NUnit,但发现了几个教程。我想测试我对 AccountController (asp.net core 2 mvc) 的修改,我使用存储库将逻辑放入此存储库。

问题是如何安排所有这些依赖注入(inject)的测试。

Controller 示例。

namespace Inside.Controllers
{
[Authorize]
[Route("[controller]/[action]")]
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ILogger _logger;
private readonly IUsersRepository _users;

public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<AccountController> logger,
IUsersRepository users
)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
_users = users;
}

[HttpGet]
[AllowAnonymous]
public IActionResult RegisterFirstUser(string returnUrl = null)
{
if (!_users.HasUsersInDB())
{
return RedirectToAction(nameof(SetupWebApp));
}
if (!_users.HasOnlySystemUser())
{
return RedirectToAction(nameof(Login)); ;
}
ViewData["ReturnUrl"] = returnUrl;
return View();
}
}

现在我不知道如何进行测试来测试这个 Controller ,因为有很多依赖注入(inject)。特别是这两个 UserManager userManager,SignInManager signInManager

抱歉新手问题。 :p

最佳答案

您可以使用 Moq很容易做到这一点。假设您的 Controller 具有此签名:

public class UsersController : Controller
{
private IUserRepository _userRepository;
private ApplicationUserManager _userManager;
private ApplicationSignInManager _signInManager;

public UsersController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IUserRepository userRepository)
{
UserManager = userManager;
SignInManager = signInManager;
_userRepository = userRepository;
}

...
}

然后对此的测试将看起来像这样(使用 Moq)

 [TestClass]
public class UsersControllerTests
{
private Mock<IUserRepository> _userRepository;
private Mock<ApplicationUserManager> _userManager;
private Mock<ApplicationSignInManager> _signInManager;
private UsersController _controller;
private Mock<IUserStore<ApplicationUser>> _userStore;
private Mock<IAuthenticationManager> _authManager;

[TestInitialize]
public void SetUp()
{
_userRepository = new Mock<IUserRepository>();

_userStore = new Mock<IUserStore<ApplicationUser>>();
_authManager = new Mock<IAuthenticationManager>();
_userManager = new Mock<ApplicationUserManager>(_userStore.Object);
_signInManager = new Mock<ApplicationSignInManager>(_userManager.Object, _authManager.Object);

_controller = new UsersController(_userManager.Object, _signInManager.Object, _userRepository.Object);

//only do this if you want to use request object in your tests
var returnUrl = new Uri("http://www.example.com");
var httpContext = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
httpContext.Setup(x => x.Request).Returns(request.Object);
request.Setup(x => x.Url).Returns(new Uri("http://localhost:123"));
var requestContext = new RequestContext(httpContext.Object, new RouteData());
_controller.Url = new UrlHelper(requestContext);
}

关于asp.net-mvc - 在 Controller 上使用依赖注入(inject)进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48918222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com