locked
How to do unit testing for httpclient request in windows store app RRS feed

  • Question

  • Hi,

    I am developing a windows store app . I want to perform unit testing on some methods which contains httpclient request.How can I achieve this.

    Thanks,

    Alok

    • Moved by Jamles Hez Friday, May 29, 2015 9:49 AM
    Thursday, May 7, 2015 1:09 PM

All replies

  • You can wrap the HttpClient in some implementation of an interface that closely resembles or is a subset of HttpClient. You will find more information and code sample here: https://nocture.dk/2013/05/21/csharp-unit-testing-classes-with-httpclient-dependence-using-autofixture/


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, May 7, 2015 3:50 PM
  • Hi Magnus,

    Thanks for replay when I open the url suggested by you its showing 404 exception.I am unable to access the url.

    Thanks,

    Alok

    Monday, May 11, 2015 6:41 AM
  • Try again as it clearly works for me: http://nocture.dk/2013/05/21/csharp-unit-testing-classes-with-httpclient-dependence-using-autofixture/

    However the idea should be straight-forward. You define an interface that declares a subset of the methods of the HttpClient class and create a concrete class that implements this interface and wraps an instance of the HttpClient class.

    Then you pass a parameter of the interface type to your class or method that uses the HttpClient class. You can then replace your concrete class with any other class that implements the same interface in your unit tests. 

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, May 11, 2015 10:45 AM
  • Sorry I tried it again but its showing 403 forbidden exception.

    Monday, May 11, 2015 12:34 PM
  • I can see the web site provided by Magnus. Is there something wrong with your network configuration?

    Thursday, May 14, 2015 8:08 AM