{"id":161,"date":"2020-10-21T13:29:53","date_gmt":"2020-10-21T12:29:53","guid":{"rendered":"https:\/\/wissing.me\/?p=161"},"modified":"2020-10-21T13:29:53","modified_gmt":"2020-10-21T12:29:53","slug":"unit-testing-mvc-responses","status":"publish","type":"post","link":"https:\/\/daan.wsng.eu\/index.php\/2020\/10\/21\/unit-testing-mvc-responses\/","title":{"rendered":"Unit Testing MVC Responses"},"content":{"rendered":"\n<p>Something I keep having to look up is testing MVC (5) responses. There are several types of responses you can return on a MVC method:<\/p>\n\n\n\n<ul><li>Plain ol&#8217; object. <\/li><li>IHttpActionResult.<\/li><li>HttpResponseMessage. <\/li><\/ul>\n\n\n\n<h2>Plain ol&#8217; object<\/h2>\n\n\n\n<p>Returning an object means that it will be converted to the format in the Request (e.g. JSON, XML). However, you have no direct control over the returned HTTP code. Example: <code>public Person Get(int personId)<\/code>. Testing this is straightforward as you immediately have the result you need. Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ Act\nPerson person = controller.Get(10);\n\n\/\/ Assert\nAssert.That(person.Id, Is.EqualTo(10));\n<\/pre><\/div>\n\n\n<h2>IHttpActionResult<\/h2>\n\n\n\n<p>You can use standard functions as a result, like <code>Ok(myObject) <\/code>or <code>BadRequest()<\/code>. Testing is straightforward by casting the return value to the expected result class. Example function: <code>public IHttpActionResult Get(int personId)<\/code>. Testing will look like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ Act\nIHttpActionResult actionResult = controller.Get(142);\n\n\/\/ Assert\nAssert.That(actionResult, Is.InstanceOf&lt;NotFoundResult&gt;());\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h2>HttpResponseMessage<\/h2>\n\n\n\n<p>This gives the most control over the response message. For example: you can set headers or ReasonPhrase. Example: <code>public HttpResponseMessage Get(int personId)<\/code>. Testing requires some extra setup such that the Response Object can be created. Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ Arrange\nPersonController controller = new PersonController();\ncontroller.Request = new HttpRequestMessage();\ncontroller.Configuration = new HttpConfiguration();\n\n\/\/ Act\nvar response = controller.Get(10);\n\n\/\/ Assert\nAssert.IsTrue(response.TryGetContentValue&lt;Person&gt;(out person));\nAssert.AreEqual(10, person.Id);\n<\/pre><\/div>\n\n\n<p>Because of the extra control of the Response we can also test the extra Response properties:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ Arrange\nPersonController controller = new PersonController();\ncontroller.Request = new HttpRequestMessage();\ncontroller.Configuration = new HttpConfiguration();\n\n\/\/ Act\nvar response = controller.Get(-1);\n\n\/\/ Assert\nAssert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));\nAssert.That(response.ReasonPhrase, Is.EqualTo(&quot;Invalid id provided&quot;));\n<\/pre><\/div>\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>So, there you have it. Three ways to test the different types of controller functions that will return an object. More information can be found at <a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/web-api\/overview\/testing-and-debugging\/unit-testing-controllers-in-web-api\">https:\/\/docs.microsoft.com\/en-us\/aspnet\/web-api\/overview\/testing-and-debugging\/unit-testing-controllers-in-web-api<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Something I keep having to look up is testing MVC (5) responses. There are several types of responses you can return on a MVC method: Plain ol&#8217; object. IHttpActionResult. HttpResponseMessage. Plain ol&#8217; object Returning an object means that it will be converted to the format in the Request (e.g. JSON, XML). However, you have no&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"twitterCardType":"","cardImageID":0,"cardImage":"","cardTitle":"","cardDesc":"","cardImageAlt":"","cardPlayer":"","cardPlayerWidth":0,"cardPlayerHeight":0,"cardPlayerStream":"","cardPlayerCodec":""},"categories":[4,10],"tags":[],"_links":{"self":[{"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/posts\/161"}],"collection":[{"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":5,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/posts\/161\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}