Need some advice for trying to mock a .NET WebClient or equivalent(需要一些建议来尝试模拟 .NET WebClient 或等效项)
问题描述
我有一些代码可以下载一些 RSS 源.
I've got some code which downloads some RSS feeds.
我一直在使用 WebClient
或 Argotic.Syndication.RssFeed
库.
I've been using WebClient
or Argotic.Syndication.RssFeed
libraries.
但这些不是可模拟的 :(
我绝对不想每次运行单元测试时都点击真正的 RSS 提要.
I definately do not want to hit the real RSS feed every time I run the unit test.
有人对我能做什么有任何建议吗?
Does anyone have any suggestions to what I can do?
我需要创建一个邪恶的包装器吗?如果是这样.. 对此有何建议?
Do I need to create an evil wrapper? If so .. suggestions on this?
推荐答案
我会为每个外部依赖项创建一个包装器(如果可行的话).因此,与文件系统/网络服务/数据库的每次交互都应仅通过接口完成,这将产生更多可测试的代码.
I go for creating a wrapper for every external dependency (if it's practical). So, every interaction with the filesystem/a webservice/database should be done through an interface only, which will lead to more testable code.
另一个解决方案当然是(一如既往)Typemock Isolator.
Another solution of course is (as always) Typemock Isolator.
这篇关于需要一些建议来尝试模拟 .NET WebClient 或等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:需要一些建议来尝试模拟 .NET WebClient 或等效项


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01