如何模拟 ASP.NET ServerVariables[“HTTP_HOST”] 值?

发布于 2024-08-22 12:19:28 字数 366 浏览 6 评论 0原文

我有以下代码,在运行时失败...

var mock = new Mock<ControllerContext>();
mock.SetupGet(x => x.HttpContext.Request
    .ServerVariables["HTTP_HOST"]).Returns(domain);

** 运行时错误:不可覆盖的设置无效 财产

我的控制器中有一些代码,它需要检查用户请求/访问的域。

我不知道如何模拟它?有什么想法吗?

附言。我在上面的例子中使用了 Moq 框架..所以我不确定这是否是一个问题,等等?

i have the following code, which fails during runtime...

var mock = new Mock<ControllerContext>();
mock.SetupGet(x => x.HttpContext.Request
    .ServerVariables["HTTP_HOST"]).Returns(domain);

** RunTime Error: Invalid setup on non-overridable
property

I've got some code in my controller, which needs to check the domain the user has requested/gone to.

I'm not sure how to mock it up? any ideas?

PS. I'm using the Moq framewoke in the above example .. so I'm not sure if that's an issue, etc.?

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

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

发布评论

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

评论(2

咆哮 2024-08-29 12:19:28

您无法模拟 NameValueCollection 上的索引器,因为它不是虚拟的。我要做的是模拟 ServerVariables 属性,因为它是虚拟的。您可以填写自己的NameValueCollection。见下文

这是我要做的:

 var context = new Mock<ControllerContext>();
 NameValueCollection variables = new NameValueCollection();
 variables.Add("HTTP_HOST", "www.google.com"); 
 context.Setup(c => c.HttpContext.Request.ServerVariables).Returns(variables);
 //This next line is just an example of executing the method 
 var domain = context.Object.HttpContext.Request.ServerVariables["HTTP_HOST"];
 Assert.AreEqual("www.google.com", domain);

You can't mock the indexer on a NameValueCollection because it's not virtual. What I would do is mock the ServerVariables property since that IS virtual. You can fill in your own NameValueCollection. See below

Here's what I would do:

 var context = new Mock<ControllerContext>();
 NameValueCollection variables = new NameValueCollection();
 variables.Add("HTTP_HOST", "www.google.com"); 
 context.Setup(c => c.HttpContext.Request.ServerVariables).Returns(variables);
 //This next line is just an example of executing the method 
 var domain = context.Object.HttpContext.Request.ServerVariables["HTTP_HOST"];
 Assert.AreEqual("www.google.com", domain);
沫雨熙 2024-08-29 12:19:28

您可以使用接口覆盖 HttpContext 并在测试中模拟它:

interface IHttpContextValues
{
    string HttpHost { get; }
}

class HttpContextValues : IHttpContextValues
{
    public string HttpHost
    {
        get { return HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; }
    }
}

class BaseController : Controller
{
    public IHttpContextValues HttpContextValues;
    BaseController()
    {
        HttpContextValues = new HttpContextValues();
    }
}

然后在控制器代码中使用 HttpContextValues 而不是 ControllerContext.HttpContext。您不必对模拟进行任何组合。

You can cover HttpContext with interface and mock it in your tests:

interface IHttpContextValues
{
    string HttpHost { get; }
}

class HttpContextValues : IHttpContextValues
{
    public string HttpHost
    {
        get { return HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; }
    }
}

class BaseController : Controller
{
    public IHttpContextValues HttpContextValues;
    BaseController()
    {
        HttpContextValues = new HttpContextValues();
    }
}

Then you use HttpContextValues instead of ControllerContext.HttpContext in your controller code. You don't have to make any combinations about mocking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文