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

后端 未结 2 1499
抹茶落季
抹茶落季 2020-12-18 18:59

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

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

Bu

相关标签:
2条回答
  • 2020-12-18 19:43

    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.

    0 讨论(0)
  • 2020-12-18 19:55

    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.

    0 讨论(0)
提交回复
热议问题