title: unit testing of very small asp.net mvc actions returning json
body: <p>The first line of my action is tested with an extra integration test.
  The second line is tested with an extra unit test for automapper mapping stuff
  The third line is untested then.</p>
  
  <p>The below unit test does it test the third line? because it just tests the return type.</p>
  
  <p>This seems stupid or too trivial too me. What else should the method return????</p>
  
  <p>There is no if/else logic inside this action. Therefore just testing for type == JsonNetResult seems superfluid for me. I would not even realize in my unit test if someone removes the success = true anonymous type.</p>
  
  <p>Should I rather test the data of the JsonNetResult with QUnit?</p>
  
  <p>I would be glad about some guidance and tips because the actions returning Json data drive me crazy... Its just fetch data from db and put it inside the JsonNetResult object.</p>
  
  <p><strong>Action</strong></p>
  
  <pre><code>[HttpGet]
  public ActionResult GetTemplateRootUnits(int templateId)
  {
      IEnumerable&lt;Unit&gt; units = _dataProvider.GetTemplateRootUnits(templateId);
      IEnumerable&lt;UnitTreeViewModel&gt; unitTreeViewModels = Mapper.Map&lt;IEnumerable&lt;Unit&gt;, IEnumerable&lt;UnitTreeViewModel&gt;&gt;(units);
      return new JsonNetResult(new { data = unitTreeViewModels, success = true });
  }
  </code></pre>
  
  <p><strong>Unit test</strong></p>
  
  <pre><code>[Test]
  public void GetTemplateRootUnits_TemplateExists_ReturnsJsonNetResult()
  {
      // ARRANGE  
      Mock&lt;IUnitDataProvider&gt; mock1 = new Mock&lt;IUnitDataProvider&gt;();
      Mock&lt;IMappingEngine&gt; mock2 = new Mock&lt;IMappingEngine&gt;();
      Mock&lt;ControllerContext&gt; mock3 = new Mock&lt;ControllerContext&gt;();
      UnitController controller = new UnitController(mock1.Object, mock2.Object);
      mock1.Setup(m =&gt; m.GetTemplateRootUnits(1)).Returns(new List&lt;Unit&gt; 
      { 
          new Unit{ UnitId = 1, Name = "Name1", HasChildren = false},
          new Unit{ UnitId = 2, Name = "Name2", HasChildren = false},
          new Unit{ UnitId = 3, Name = "Name3", HasChildren = false},
      });
  
      var unitTreeViewModels = new List&lt;UnitTreeViewModel&gt; 
      { 
          new UnitTreeViewModel { Id = 1, Name = "Name1", HasChildren = false},
          new UnitTreeViewModel { Id = 2, Name = "Name2", HasChildren = false},
          new UnitTreeViewModel { Id = 3, Name = "Name3", HasChildren = false},
      };
  
      // Thats the way AutoMapper is mocked
      mock2.Setup(m =&gt; m.Map&lt;IEnumerable&lt;Unit&gt;, IEnumerable&lt;UnitTreeViewModel&gt;&gt;(It.IsAny&lt;IEnumerable&lt;Unit&gt;&gt;())).Returns(unitTreeViewModels);
  
      // ACT
      ActionResult result = controller.GetTemplateRootUnits(1);
  
      // ASSERT - check that the dataProvider.GetTestplanRootUnits() was called
      mock1.Verify(m =&gt; m.GetTemplateRootUnits(1));
  
      // ASSERT
      Assert.IsInstanceOfType(typeof(JsonNetResult), result);
  } 
  </code></pre>
  
  <p><strong>JsonNetResult.cs</strong></p>
  
  <pre><code>public class JsonNetResult : ContentResult
      {
          private readonly object _data;
          public JsonNetResult(object data)
          {
              _data = data;
          }
  
          public override void ExecuteResult(ControllerContext context)
          {
              Content = JsonConvert.SerializeObject(_data);
              ContentType = "application/json";
              base.ExecuteResult(context);
          }
          public object Data { get { return _data; } }
      }
  </code></pre>