gpt4 book ai didi

c# - UrlHelper 模拟不起作用

转载 作者:行者123 更新时间:2023-11-30 17:36:32 25 4
gpt4 key购买 nike

我正在尝试编写一个通过的单元测试,但我的测试调用的方法调用了另一个使用 UrlHelper 类生成 URL 的方法。调用 urlHelper.Action 的结果一直返回 null

我已经尝试模拟(使用 Moq)所有组件,我认为这个 Controller 将需要能够让它工作,但我仍然得到 null

单元测试类

[TestFixtureSetUp]
public void Configure_Defaults()
{

var mockHttpContextBase = new Mock<HttpContextBase>();

var mockRequest = new Mock<HttpRequestBase>();

var mockControllerContext = new Mock<ControllerContext>();

var mockWebRoutingRequestContrext = new Mock<RequestContext>();

mockRequest
.Setup(request => request.Url)
.Returns(new Uri("http://dev.fleepos.local/Register"));

mockHttpContextBase
.Setup(httpContext => httpContext.Request)
.Returns(mockRequest.Object);

mockWebRoutingRequestContrext
.Setup(request => request.HttpContext)
.Returns(mockHttpContextBase.Object);

mockWebRoutingRequestContrext
.Setup(request => request.RouteData)
.Returns(new RouteData());

var urlHelper = new UrlHelper(mockWebRoutingRequestContrext.Object);

mockControllerContext
.Setup(controllerContext => controllerContext.HttpContext)
.Returns(mockHttpContextBase.Object);


_registerController = new RegisterController() {ControllerContext = mockControllerContext.Object, Url = urlHelper};

}

[Test]
public void Display_Validate_Account_Page_On_Successful_Registration()
{
//act
var result = (RedirectToRouteResult)_registerController.Register(_userRegisterationViewModel);

//assert
Assert.That(result.RouteValues["action"], Is.EqualTo("ValidateAccount"));
}

Controller Action 调用的 Controller 方法

private string GenerateActionLink(string actionName, string token, string username)
{
string validationLink = null;
if (Request.Url != null)
{
var urlHelper = new UrlHelper(ControllerContext.RequestContext);
validationLink = urlHelper.Action(actionName, "Register",
new { Token = token, Username = username },
Request.Url.Scheme);
}

return validationLink;
}

最佳答案

Controller 已经有一个 UrlHelper Url 属性,可以模拟它来做你想做的事。通过在 Controller 操作调用的 Controller 方法中创建一个 new UrlHelper,就失去了替换 mock/fake 的机会。

首先更新controller action调用的controller方法使其测试友好

private string GenerateActionLink(string actionName, string token, string username) {
string validationLink = null;
if (Request.Url != null) {
validationLink = Url.Action(actionName, "Register",
new { Token = token, Username = username },
Request.Url.Scheme);
}

return validationLink;
}

现在 Controller 的 UrlHelper 有了更多的控制权。 UrlHelper 也可以被模拟并传递给 Controller ​​上下文

这是上面解释的最小完整示例。

[TestClass]
public class UrlHelperTest {
[TestMethod]
public void MockUrlHelper() {
//Arrange
var requestUrl = new Uri("http://myrequesturl");
var request = Mock.Of<HttpRequestBase>();
var requestMock = Mock.Get(request);
requestMock.Setup(m => m.Url).Returns(requestUrl);

var httpcontext = Mock.Of<HttpContextBase>();
var httpcontextSetup = Mock.Get(httpcontext);
httpcontextSetup.Setup(m => m.Request).Returns(request);


var actionName = "MyTargetActionName";
var expectedUrl = "http://myfakeactionurl.com";
var mockUrlHelper = new Mock<UrlHelper>();
mockUrlHelper
.Setup(m => m.Action(actionName, "Register", It.IsAny<object>(), It.IsAny<string>()))
.Returns(expectedUrl)
.Verifiable();

var sut = new MyController();
sut.Url = mockUrlHelper.Object;
sut.ControllerContext = new ControllerContext {
Controller = sut,
HttpContext = httpcontext,
};

//Act
var result = sut.MyAction();

//Assert
mockUrlHelper.Verify();
}

public class MyController : Controller {
[HttpPost]
public ActionResult MyAction() {
var link = GenerateActionLink("MyTargetActionName", string.Empty, string.Empty);

return View((object)link);
}

private string GenerateActionLink(string actionName, string token, string username) {
string validationLink = null;
if (Request.Url != null) {
var encodedToken = EncodedUrlParameter(token);
var url = Url.Action(actionName, "Register", new { Token = encodedToken, Username = username }, Request.Url.Scheme);
validationLink = url;
}

return validationLink;
}

private string EncodedUrlParameter(string token) {
return "Fake encoding";
}

}
}

关于c# - UrlHelper 模拟不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39292414/

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