How to Unit Test calling Rest Url (http://localhost:1234)? RRS feed

  • Question

  • User600651647 posted


    I have an API controller which calls a Rest Url (http://localhost:1234). 

    I want to Unit Test it and make sure that it returns the correct values.

    I need to pass Header Values like Content Type etc.


    Monday, April 16, 2018 6:36 PM

All replies

  • User1120430333 posted



    •It talks to the database
    •It communicates across the network
    •It touches the file system
    •It can't run at the same time as any of your other unit tests
    •You have to do special things to your environment (such as editing config files) to run it.


    You need to mock out the call to an external source, like a call to a Rest Url. But can you do it from a controller that is making a direct call to the external source? That's probably going to be a no. However, you could mock out the call to a Repository that made the call to the RestUrl on the behalf of the controller that called the Repository.



    Mock objects are instances of test-provided classes that simulate the behavior of external components. Mock objects isolate the application code under test.




    It centralizes the data logic or Web service access logic.
    It provides a substitution point for the unit tests.


    Monday, April 16, 2018 8:48 PM
  • User36583972 posted

    Hi robinspaul,

    Please refer the following article to create simple unit tests for your Web API 2 application.

    Unit Testing ASP.NET Web API 2

    Best Regards,

    Yong Lu

    Tuesday, April 17, 2018 8:36 AM