如何伪造 HttpContext 进行单元测试?
我需要伪造 HttpContext.Current.Application
表才能从单元测试中访问它。
我需要将我的数据存储在某个地方。我以为我可以只传递 NameValueCollectionBase
的实例,但我发现这个基本类型没有索引器,所以使用起来似乎太复杂了。
那么伪造 HttpContext
的这一部分怎么样?是否可以?我怎样才能做到呢? NUnit.Mocks
有帮助吗?
先感谢您...
I need to fake HttpContext.Current.Application
table in order to access it from my unit tests.
I need to store my data somewhere. I thought that I can just pass instance of NameValueCollectionBase
but as I discovered this base type has no indexer so it seems too complicated to use.
So what about faking this part of HttpContext
? Is it possible? How can I make it? Will be NUnit.Mocks
helpful?
Thank you in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请浏览下面的链接,它会对您有所帮助。
http:// www.java2s.com/Open-Source/CSharp/Web-Frameworks/MvcContrib/MvcContrib/TestHelper/Fakes/FakeHttpContext.cs.htm
模拟和 HttpContextBase.get_User()
谢谢
文卡特
Please go through below links it will help you.
http://www.java2s.com/Open-Source/CSharp/Web-Frameworks/MvcContrib/MvcContrib/TestHelper/Fakes/FakeHttpContext.cs.htm
Mocking and HttpContextBase.get_User()
Thanks
Venkat
如果您需要 namevaluecollection 基础的索引,请使用下面的代码,
如果只是为了存储数据并传递测试方法,请使用上面的代码。
If you need indexes for namevaluecollection base please use below code
For just to store data and passing around test methods please use above code.
在这种情况下,我生成一些从 System.Web.Abstractions 中的基类派生的存根。我经常在 MVC 应用程序中使用这种技术,因为 MVC / WebApi 控制器包含对 HttpContext (HttpContextBase) 的抽象,
这样我就可以在我的单元/集成测试中消除 HttpContext 需求,这是一个示例...
然后我的测试可以建立 Controller 和 Http上下文:
我的控制器知道它的环境 HttpContextBase 实例提供缓存和应用程序状态:
In this scenario I generate some stubs derived from the base classes in System.Web.Abstractions. I often use this technique for MVC applications as MVC / WebApi controllers contain an abstraction to HttpContext (HttpContextBase)
This way I can stub out HttpContext requirements in my unit / integration tests, here's a sample...
Then my test can establish Controller and Http Context:
And my controller is aware of it's ambient HttpContextBase instance supplying Cache and Application state: