Last post Apr 17, 2018 08:36 AM by Yohann Lu
Apr 16, 2018 06:36 PM|robinspaul|LINK
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.
Apr 16, 2018 08:48 PM|DA924|LINK
•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.
Apr 17, 2018 08:36 AM|Yohann Lu|LINK
Please refer the following article to create simple unit tests for your Web API 2 application.
Unit Testing ASP.NET Web API 2