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

  • Question

  • User600651647 posted

    Hi,

    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.

    Thanks

    Monday, April 16, 2018 6:36 PM

All replies

  • User1120430333 posted

    https://www.artima.com/weblogs/viewpost.jsp?thread=126923

    <copied>

    •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.

    <end>

    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.

    https://msdn.microsoft.com/en-us/library/ff650441.aspx

    <copied>

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

    <end>

    https://msdn.microsoft.com/en-us/library/ff649690.aspx

    <copied>

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

    <end>

    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