惩戒HttpContext.GetGlobalResourceObject在NUnit的起订量与 [英] Mocking HttpContext.GetGlobalResourceObject in NUnit with Moq
问题描述
我想写哪些测试使用HttpContext.GetGlobalResourceObject的方法(单元测试),我想它使用起订量来嘲笑。
比方说,我们再测试此方法:
公开名单< DctmGridColumn> GetDctmColumnsMandatory()
{
名单,LT; DctmGridColumn> metadataFields =新的List< DctmGridColumn>
{
新DctmGridColumn(HttpContext.GetGlobalResourceObject(SharePoint.Service,DctmGridColumn_DispName_r_object_id)。的ToString()),
新DctmGridColumn(HttpContext.GetGlobalResourceObject(SharePoint.Service, DctmGridColumn_DispName_object_name)。的ToString()),
新DctmGridColumn(HttpContext.GetGlobalResourceObject(SharePoint.Service,DctmGridColumn_DispName_r_modify_date)。的ToString()),
新DctmGridColumn(HttpContext.GetGlobalResourceObject(SharePoint中。服务,DctmGridColumn_DispName_r_version_label)的ToString())
}。
返回metadataFields;
}
这是我的测试:
[测试]
公共无效TestGetDctmColumnsMandatory_IsNotNull()
{
无功柱= _viewDefinitionOperations.GetDctmColumnsMandatory();
Assert.IsNotNull(列);
}
我怎么能嘲笑的HttpContext?我一直在谷歌上搜索了整整一天,我发现一两个例子用嘲讽的框架,其余将创建自己的嘲弄。我试图用设置当前的HttpContext标记为答案的方法会议单元测试,但后来我不得不与HttpContextFactory.Current替换为HttpContext.Current任何电话,并获得相同的方法。我不是与解决方案舒适。
我是相当新的嘲讽,但我想必须有一个更简单的方法?
我只是想直接从单元测试来模拟我的班HttpContext的所有我的电话。那是要多问? :)
-
有测试下你的类采取<$ C $的依赖C> HttpContextBase
(即摘要
)。 -
在正常执行使用
HttpContextWrapper
(从
派生HttpContextBase
)来包装的HttpContext
和供应,为您的
类。 -
然后在你的单元测试,你可以在一个模拟
HttpContextBase
,其通过GetGlobalResourceObject
你掌控。
这很简单,但它需要你重新思考如何设计应用程序的一部分。这种控制反转的是写代码,很容易进行单元测试是必不可少的。它可以帮助使用IoC容器。
I am trying to write a unit test which tests a method that uses HttpContext.GetGlobalResourceObject() and I would like to mock it using Moq.
Let's say we're testing this method:
public List<DctmGridColumn> GetDctmColumnsMandatory()
{
List<DctmGridColumn> metadataFields = new List<DctmGridColumn>
{
new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_r_object_id").ToString()),
new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_object_name").ToString()),
new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_r_modify_date").ToString()),
new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_r_version_label").ToString())
};
return metadataFields;
}
And this is my test:
[Test]
public void TestGetDctmColumnsMandatory_IsNotNull()
{
var columns = _viewDefinitionOperations.GetDctmColumnsMandatory();
Assert.IsNotNull(columns);
}
How can I mock HttpContext? I've been googling the whole day, and I found one or two examples using a mocking framework, the rest creates their own mocks. I've tried to use the approach marked as answer in Setting the httpcontext current session in unit test, but then I'll have to "replace any calls to HttpContext.Current with HttpContextFactory.Current and have access to the same methods." I'm not that comfortable with that solution.
I'm fairly new to mocking, but I guess there has to be an easier way? I just want to direct all my calls to HttpContext from the unit tests to my mock class. Is that to much to ask? :)
Have your class under test take a dependency on
HttpContextBase
(which isabstract
).In normal execution use an
HttpContextWrapper
(which derives fromHttpContextBase
) to wrap theHttpContext
and supply that to your class.Then in your unit tests you can pass in a mock
HttpContextBase
whoseGetGlobalResourceObject
you control.
It is simple but it requires you to rethink how you design parts of your application. This kind of inversion of control is essential to writing code that's easy to unit test. It may help to use an IoC container.
这篇关于惩戒HttpContext.GetGlobalResourceObject在NUnit的起订量与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!