Need some advice for trying to mock a .NET WebClient or equivalent

懵懂的女人 提交于 2019-11-28 02:34:03

问题


I've got some code which downloads some RSS feeds.

I've been using WebClient or Argotic.Syndication.RssFeed libraries.

But these aren't mockable :(

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?


回答1:


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.

Another solution of course is (as always) Typemock Isolator.




回答2:


Does anyone have any suggestions to what I can do?

Abstract the actual HTTP call behind an interface. Then write an implmentation of this interface using WebClient. Now have your main class take this interface as dependency. You could now mock this interface in the unit test and configure your dependency injection framework inject the correct implementation at runtime.



来源:https://stackoverflow.com/questions/11095557/need-some-advice-for-trying-to-mock-a-net-webclient-or-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!