由于预期的 cookie,模拟 httpContext 和会话的 nSubstitute 问题
nSubstitute issue mocking httpContext and the session due to expected cookie
我有一个 MVC 应用程序,我试图在其中模拟我的家庭控制器,以便调用我的 controller.Index()
。
问题出在我的 homeController 的构造函数上。这是实际代码:
public HomeController(IHttpContextAccessor httpContext, IUserRepository<CompanyUser> userRepository, IAuthorisationChecks authorisationChecks) : base(httpContext, userRepository, authorisationChecks)
{
}
我的基地 class 有以下内容:
public ControllerBase(IHttpContextAccessor httpContext,IUserRepository<CompanyUser> userRepository,IAuthorisationChecks authorisationChecks)
{
_authorisationChecks = authorisationChecks;
_userRepository = userRepository;
_httpContext = httpContext;
var cookie = _httpContext.HttpContext.Request.Cookies["auth"];
var userCookie = _authorisationChecks.UnencryptedCookieValue("userdata", cookie);
var userDeserialised = JsonConvert.DeserializeObject<SignInModel>(userCookie);
userDeserialised.SessionId = _httpContext.HttpContext.Session.Id;
var loggedInUser = Task.Run(async () => await _userRepository.SignInAsync(userDeserialised)).Result;
_httpContext.HttpContext.Session.SetCurrentUser(loggedInUser);
}
在我的测试中我目前有:
[Fact]
public void Index_Returns_Correct_View_Test()
{
// Arrange
var httpRequest = Substitute.For<HttpRequest>();
var session = Substitute.For<ISession>();
var httpResponse = Substitute.For<HttpResponse>();
var httpContext = Substitute.For<IHttpContextAccessor>();
var userRepository = Substitute.For<IUserRepository<EvaluateUser>>();
var authorisationChecks = Substitute.For<IAuthorisationChecks>();
userRepository.SignInAsync(new SignInModel
{
EmailAddress = _configuration["user:emailAddress"],
Password = _configuration["user:password"],
UserName = _configuration["user:username"],
SessionId = _configuration["user:sessionId"]
});
session.Id.Returns(_configuration["user:sessionId"]);
httpContext.HttpContext.Request.Returns(httpRequest);
httpContext.HttpContext.Session.Returns(session);
httpContext.HttpContext.Response.Returns(httpResponse);
var controller = new HomeController(httpContext, userRepository, authorisationChecks);
// Act
var result = controller.Index();
// Assert
Assert.NotNull(result);
}
当我尝试 运行 测试时,它在 controllerBase 中尝试设置 userDeserialised.SessionID
的行上失败,因为 var cookie
是空的。我怎样才能将其模拟到我的测试中,以便我的测试能够通过?
您应该能够删除 cookie 内容,然后 _authorisationChecks
调用:
val authCookie = "my-cookie-content";
httpContext.HttpContext.Request.Cookies["auth"].Returns(authCookie);
val userCookie = "{ json serialised sign-in model here }";
_authorisationChecks.UnencryptedCookieValue("userdata", authCookie).Returns(userCookie);
我有一个 MVC 应用程序,我试图在其中模拟我的家庭控制器,以便调用我的 controller.Index()
。
问题出在我的 homeController 的构造函数上。这是实际代码:
public HomeController(IHttpContextAccessor httpContext, IUserRepository<CompanyUser> userRepository, IAuthorisationChecks authorisationChecks) : base(httpContext, userRepository, authorisationChecks)
{
}
我的基地 class 有以下内容:
public ControllerBase(IHttpContextAccessor httpContext,IUserRepository<CompanyUser> userRepository,IAuthorisationChecks authorisationChecks)
{
_authorisationChecks = authorisationChecks;
_userRepository = userRepository;
_httpContext = httpContext;
var cookie = _httpContext.HttpContext.Request.Cookies["auth"];
var userCookie = _authorisationChecks.UnencryptedCookieValue("userdata", cookie);
var userDeserialised = JsonConvert.DeserializeObject<SignInModel>(userCookie);
userDeserialised.SessionId = _httpContext.HttpContext.Session.Id;
var loggedInUser = Task.Run(async () => await _userRepository.SignInAsync(userDeserialised)).Result;
_httpContext.HttpContext.Session.SetCurrentUser(loggedInUser);
}
在我的测试中我目前有:
[Fact]
public void Index_Returns_Correct_View_Test()
{
// Arrange
var httpRequest = Substitute.For<HttpRequest>();
var session = Substitute.For<ISession>();
var httpResponse = Substitute.For<HttpResponse>();
var httpContext = Substitute.For<IHttpContextAccessor>();
var userRepository = Substitute.For<IUserRepository<EvaluateUser>>();
var authorisationChecks = Substitute.For<IAuthorisationChecks>();
userRepository.SignInAsync(new SignInModel
{
EmailAddress = _configuration["user:emailAddress"],
Password = _configuration["user:password"],
UserName = _configuration["user:username"],
SessionId = _configuration["user:sessionId"]
});
session.Id.Returns(_configuration["user:sessionId"]);
httpContext.HttpContext.Request.Returns(httpRequest);
httpContext.HttpContext.Session.Returns(session);
httpContext.HttpContext.Response.Returns(httpResponse);
var controller = new HomeController(httpContext, userRepository, authorisationChecks);
// Act
var result = controller.Index();
// Assert
Assert.NotNull(result);
}
当我尝试 运行 测试时,它在 controllerBase 中尝试设置 userDeserialised.SessionID
的行上失败,因为 var cookie
是空的。我怎样才能将其模拟到我的测试中,以便我的测试能够通过?
您应该能够删除 cookie 内容,然后 _authorisationChecks
调用:
val authCookie = "my-cookie-content";
httpContext.HttpContext.Request.Cookies["auth"].Returns(authCookie);
val userCookie = "{ json serialised sign-in model here }";
_authorisationChecks.UnencryptedCookieValue("userdata", authCookie).Returns(userCookie);