title: Moq setup returns null
body: <p>So, I'm starting to learn an implement UniTesting on a Web Api project I'm working on. What's happening is that when I setup a mock a call this one returns null instead of the value I'm telling to returns. I don't know why a similiar Setup works but this one just doesn't.</p>
  
  <p>Here is my Test Class</p>
  
  <pre><code>namespace API.Tests.Web
  {
      [TestClass]
      public class MaterialsControllerTest
      {
          private MaterialsController controller;
          private Mock&lt;IRTWRepository&gt; repository;
          private Mock&lt;IModelFactory&gt; factory;
          private Mock&lt;IRTWAPIIdentityService&gt; identityService;
          List&lt;MaterialAccepted&gt; materials;
          MaterialAccepted material;      
  
          [TestInitialize]
          public void Initialize()
          {
              repository = new Mock&lt;IRTWRepository&gt;();
              factory = new Mock&lt;IModelFactory&gt;();          
              identityService = new Mock&lt;IRTWAPIIdentityService&gt;();
              controller = new MaterialsController(repository.Object);
              material = new MaterialAccepted()
              {
                  business = true,
                  businessService = EnumRecycleCenterService.Dropoff,
                  residential = false,
                  residentialService = EnumRecycleCenterService.Pickup,
                  note = "this a note",
                  Category = new Category()
                  {
                      name = "Books"
                  }
              };
  
              materials = new List&lt;MaterialAccepted&gt;()
                  {
                      new MaterialAccepted() { business=true,businessService=EnumRecycleCenterService.Dropoff,residential=false,residentialService=EnumRecycleCenterService.Pickup,note="this a note"},
                      new MaterialAccepted() { business=false,businessService=EnumRecycleCenterService.Dropoff,residential=true,residentialService=EnumRecycleCenterService.Pickup,note="this a note"},
                  };
          }    
  
          [TestMethod]        
          public void Post_ShouldReturnBadRequestWhenMaterialAcceptedModelValidationFails()
          {            
              //arrange
              repository.Setup(r =&gt; r.RecycleCenterRepository.Get(3)).Returns(() =&gt; new RecycleCenter());
              controller.ModelState.AddModelError("error", "unit test error");
              //act
  
              var actionResult = controller.Post(2, new MaterialAcceptedModel());
  
              Assert.IsInstanceOfType(actionResult, typeof(BadRequestResult));
          }                                        
      }
  }
  </code></pre>
  
  <p>Here is the action in the Controller I'm trying to test</p>
  
  <pre><code>[HttpPost]
          [Route("api/recyclecenters/{rcid}/materials/")]
          public IHttpActionResult Post(int rcid, [FromBody]MaterialAcceptedModel model)
          {
              try
              {
                  if (model != null)
                  {
                      var recycleCenter = TheRepository.RecycleCenterRepository.Get(rcid);
  
                      if (recycleCenter == null)
                          return NotFound();
  
                      if (!ModelState.IsValid)
                          return BadRequest(ModelState);
  
                      var entity = TheModelFactory.Parse(model);
  
                      if (entity == null) return BadRequest("Could not read material accepted in body");
  
                      if (TheRepository.MaterialAcceptedRepository.Get(recycleCenter.RecycleCenterId, entity.Category.name) != null)
                          return Conflict();
  
                      recycleCenter.Materials.Add(entity);
  
                      if (TheRepository.SaveAll())
                      {
                          string locationHeader = Url.Link("Materials", new { rcid = rcid, name = model.category.ToLower() });
                          return Created&lt;MaterialAcceptedModel&gt;(locationHeader, TheModelFactory.Create(entity));
                      }
                      return BadRequest("Could not save to the database");
                  }
                  return BadRequest();
              }
              catch (Exception ex)
              {
                  return BadRequest(ex.Message);
              }    
          }
  </code></pre>
  
  <p>If I run this test it will fail because it returns an instancy type of NotFoundResult instead of a BadRequestResult, and this is happening because the test method stops in this line</p>
  
  <pre><code>if (recycleCenter == null)
      return NotFound();
  </code></pre>
  
  <p>But this test it suppose to stop on this line</p>
  
  <pre><code> if (!ModelState.IsValid)
        return BadRequest(ModelState);
  </code></pre>
  
  <p>Any ideas why this</p>
  
  <pre><code> repository.Setup(r =&gt; r.RecycleCenterRepository.Get(3)).Returns(() =&gt; new RecycleCenter());
  </code></pre>
  
  <p>is returning null when it should return a new RecycleCenter </p>