title: Unit Testing - Interfaces, Mocking and Dependancy Injection
body: <p>Im trying to get my head around some aspects of Unit testing, so I've created a simple solution with the following architecture:</p>
  
  <p><a href="https://i.stack.imgur.com/WjUOa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WjUOa.jpg" alt="enter image description here"></a></p>
  
  <p>In the Business layer I've created a FizzBuzz class (FizzBuzzService) that implements an IFizzBuzz interface.</p>
  
  <pre><code>namespace SampleApp2017.Business
  {
  public class FizzBuzzService : IFizzBuzz
  {
      public bool IsFizzBuzz(int num)
      {
          if (num % 3 == 0 || num % 6 == 0)
          {
              return true;
          }
          else
          {
              return false;
          }
      }
  
      public bool IsFizz(int num)
      {
         return (num % 3 == 0 ? true : false);
      }
  
      public bool IsBuzz(int num)
      {
          if (num % 6 == 0)
          {
              return true;
          }
          else
          {
              return false;
          }
      }
  }
  }
  </code></pre>
  
  <p>(and heres the Interface details for reference)</p>
  
  <pre><code>namespace SampleApp2017.Business
  {
  public interface IFizzBuzz
  {
  
      bool IsFizzBuzz(int num);
  
      bool IsFizz(int num);
  
      bool IsBuzz(int num);
  }
  }
  </code></pre>
  
  <p>In this sample solution, the FizzBuzz service is consumed by both the WebUI and the ConsoleApp using Dependancy Injection, I understand why this is necessary however when it comes to Unit Testing the service i'm getting confused.</p>
  
  <p>Below is a test I have set up for the FizzBuzz method:</p>
  
  <pre><code>  [TestMethod]
      public void fizzbuzz_should_return_true()
      {
          //Arrange
          //Bring in the business rules
          IFizzBuzz _fbservice = new FizzBuzzService();
  
          //Act
          /// Send 12 to service, should return true
          bool resultof12 = _fbservice.IsFizzBuzz(12);
  
          //Assert
          Assert.AreEqual(resultof12, true);
      }
  </code></pre>
  
  <p>I'm now looking for some guidance on the 'best practice' for running unit tests.</p>
  
  <ol>
  <li>Does the above test look OK? (it runs fine)</li>
  <li>Is is common to use DI with Unit Testing? Surely I am testing the concrete implentation of a class?</li>
  <li><p>In unit testing do I need to programme to an interface? What would be the problem with:</p>
  
  <p>FizzBuzzService _fbservice = new FizzBuzzService();</p></li>
  <li><p>And finally, if I wanted to Mock in this example, where does it come in and what are the advantages?</p></li>
  </ol>