Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Unit Testing IHttpActionResult Created method

$
0
0

I have a Web API method that I am calling from an MVC Controller and I need to know how to Unit Test my project from my Service Layer as well as from my Presentation Layer.

My Web API method looks like this:

public IHttpActionResult Post([FromBody]ModelRequest request)
        {
            string location = string.Empty;
            try
            {
                string messageValue = "someValue"
                location = string.Format("{0}/{1}", someValue);
                return Created(location, messageValue);
            }//try
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }//catch
        }

I am using Moq as my Unit Testing mocking framework and using standard MSTest for my Visual Studio Unit Testing framework. 

Please advise as to how to Unit Test my Web API method in both my Service Layer Unit Tests as well as my Presentation Layer (MVC Application) Unit Tests.  I have no idea how to properly Mock the CreatedContentResult in my Unit Tests since I cannot see how to create a new instance of it nor how to directly invoke it through Moq.

Thanks.


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>