title: Am I writing my unit tests correctly? NUnit + NSubstitute
body: <p>I recently started learning how to write unit tests, and what part of the unit to test for functionality and what to mock out. I'm using NSubstitute as my mocking framework. My example basically calls a repo class, which then makes an WEB API web call to an external service, ie: AddCreditCard, which then returns a result. I created 2 unit tests for AddCreditCard, one for Success, and one for Fail. I'm still not 100% sure I'm doing all of this correctly. The unit tests are passing, but im not sure if my Asserts are done on the correct data... All help and suggestions welcome!</p>
  
  <pre><code>public interface IApiResponse&lt;T&gt;
  {
      HttpStatusCode StatusCode { get; set; }
      T Result { get; set; }
      string ErrorMessage { get; }
      bool HasErrors { get; }
  }
  
  public interface ISignedRequest
  {
      void ConfigureSettings(SignedRequestSettings settings);
  
      IApiResponse Post(string jsonData, Dictionary&lt;string, string&gt; parameters = null,
          IOptionalHeaders optionalHeaders = null);
  }
  
  
  public class PaymentRepository
  {
      private readonly SignedRequestSettings _settings;
      private readonly ISignedRequest _signedRequest;
  
      public PaymentRepository(ISignedRequest signedRequest = null)
      {
          if (signedRequest == null)
              _signedRequest = new SignedRequest();
          else
              _signedRequest = signedRequest;
      }
  
      public IApiResponse AddCreditCard(CreditCard request)
      {
          var jsonData =
              JsonConvert.SerializeObject(request);
  
          string action = string.Format("customers/{0}/paymentmethods", request.ConnectId);
          _settings.Url = string.Format("{0}{1}", String.Empty, action);
          _signedRequest.ConfigureSettings(_settings);
  
          return _signedRequest.Post(jsonData);
      }
   }
  
      [Test]
      public void AddCreditCard_GivenValidCreditCard_ReturnsCreatedResult()
      {
          //Given
          var apiResponse = Substitute.For&lt;IApiResponse&gt;();
          apiResponse.StatusCode = HttpStatusCode.Created;
  
          var signedRequest = Substitute.For&lt;ISignedRequest&gt;();
          signedRequest.Post(Arg.Any&lt;String&gt;()).Returns(apiResponse);
  
          var creditCard = Substitute.For&lt;CreditCard&gt;();
          creditCard.ConnectId = Guid.Parse("1fc1ad83-cd4e-4b68-bce6-e03ee8f47fb6");
  
          var repo = new PaymentRepository(signedRequest);
  
          //When
          var addCreditCardResponse = repo.AddCreditCard(creditCard);
  
          //Then
          signedRequest.Received(1).ConfigureSettings(Arg.Any&lt;SignedRequestSettings&gt;());
          signedRequest.Received(1).Post(Arg.Any&lt;String&gt;());
  
          Assert.AreEqual(HttpStatusCode.Created, addCreditCardResponse.StatusCode);
      }
  
  [Test]
      public void AddCreditCard_GivenInvalidCreditCard_ReturnsHasErrorsResult()
      {
          //Given
          var apiResponse = Substitute.For&lt;IApiResponse&gt;();
          apiResponse.HasErrors.Returns(true);
          apiResponse.ErrorMessage.Returns("Add credit card error message");
  
          var signedRequest = Substitute.For&lt;ISignedRequest&gt;();
          signedRequest.Post(Arg.Any&lt;String&gt;()).Returns(apiResponse);
  
          var creditCard = Substitute.For&lt;CreditCard&gt;();
          creditCard.ConnectId = Guid.Parse("1fc1ad83-cd4e-4b68-bce6-e03ee8f47fb6");
  
          var repo = new PaymentRepository(signedRequest);
  
          //When
          var addCreditCardResponse = repo.AddCreditCard(creditCard);
  
          //Then
          signedRequest.Received(1).ConfigureSettings(Arg.Any&lt;SignedRequestSettings&gt;());
          signedRequest.Received(1).Post(Arg.Any&lt;String&gt;());
  
          Assert.AreEqual(apiResponse.HasErrors, addCreditCardResponse.HasErrors);
          Assert.AreEqual(apiResponse.ErrorMessage, addCreditCardResponse.ErrorMessage);
      }
  </code></pre>