title: Web API Controller Unit Testing
body: <p>I have an N-tier project that am trying to test the API controller but the flow is a little bit more complex which makes me ask for some opinion from you guys... Please any suggestion will be appreciated so much...</p>
  
  <p>I'm getting an error</p>
  
  <blockquote>
    <p>Object reference not Set to An Instance Of An Object </p>
  </blockquote>
  
  <p>This is my test class and method that get all fake symptoms </p>
  
  <pre class="lang-cs prettyprint-override"><code>[TestClass]
  public class SymptomsControllerTest 
  {
  
      private Mock&lt;IAdministrationManagementService&gt; _administrationManagementServiceMock;
      SymptomsController objController;
      IList&lt;SymptomObject&gt; listSymptoms;
  
      [TestInitialize]
      public void Initialize()
      {
          _administrationManagementServiceMock = new Mock&lt;IAdministrationManagementService&gt;();
          objController = new SymptomsController(_administrationManagementServiceMock.Object);
  
          listSymptoms = new List&lt;SymptomObject&gt;() {
           new SymptomObject() { Name = "Head1" },
           new SymptomObject() { Name = "Head2" },
           new SymptomObject() { Name = "Head3" }
          };
      }
  
      [TestMethod]
      public void Symptom_Get_All()
      {
          //Arrange
          _administrationManagementServiceMock.Setup(x =&gt; x.GetSymptoms()).ReturnsAsync(listSymptoms);
  
          //Act
          var result = objController.Get() as List&lt;SymptomObject&gt;;
  
          //Assert
          Assert.AreEqual(result.Count, 3);
          Assert.AreEqual("Head1", result[0].Name);
          Assert.AreEqual("Head2", result[1].Name);
          Assert.AreEqual("Head3", result[2].Name);
  
      }
  
  }
  </code></pre>
  
  <p>the service am trying to communicate to looks likes this</p>
  
  <pre class="lang-cs prettyprint-override"><code> public Task&lt;IList&lt;SymptomObject&gt;&gt; GetSymptoms()
      {
          return Task.Run(() =&gt;
          {
              try
              {
                  using (var _uow = new HomasUoW())
                  {
                      var entities = _uow.Symptoms.GetAll().Where(x =&gt; x.isDelete == false);
  
                      if (entities.Count() &gt; 0)
                      {
                          IList&lt;SymptomObject&gt; list = new List&lt;SymptomObject&gt;();
                          foreach (var entity in entities)
                          {
                              var obj = AutoMapper.Mapper.DynamicMap&lt;Symptom, SymptomObject&gt;(entity);
  
                              obj.Name = entity.Name;
  
                              list.Add(obj);
  
  
                          }
  
                          return list;
                      }
                      else
                      {
                          throw new InvalidOperationException(Resources.NonExistingObjectRetrievalError);
                      }
                  }
  
              }
              catch (Exception)
              {
  
                  throw;
              }
          });
  
      }
  </code></pre>
  
  <p>and the API Controller looks like this </p>
  
  <pre class="lang-cs prettyprint-override"><code> public IHttpActionResult Get()
      {
          try
          {
              var symptoms = _Service.GetSymptoms();
  
              if (symptoms != null)
              {
                  return Ok(symptoms);
              }
  
              return NotFound();
          }
          catch (Exception ex)
          {
  
              return InternalServerError(ex);
          }
      }
  </code></pre>
  
  <p>Please look at it carefully and check if am missing any thing that is not allowing the test to pass.</p>