title: Correct method for testing for an exception using Moq and MSTest
body: <p>A little confusion as to the behaviour of Moq with MsTest.</p>
  
  <p>Edit: This is not a question of "How do I test?" or "How do I assert?", this is a scratch pad to see how MoQ works so don't focus on the exception type etc. </p>
  
  <p>I think a better question may be => "Does Moq Throws&lt;> behave similar to MsTest ExpectedExceptionAttribute?" That is, they're expecting an exception in the test or the SUT?</p>
  
  <p>I'd like to know just how MoQ "Throws" works when used with MsTest. Is it better to not use the MsTest expected exception attribute? Is it better to perform a try..catch within the test? I have a few more questions surrounding this.</p>
  
  <p>I am Mocking a database call and when an error occurs I would like to return zero (0).</p>
  
  <p>The TestMethod is straight forward with the MsTest exception attribute, and the throws exception with Moq. It only works when I throw an exception within the SaveCart method and not when I return zero.</p>
  
  <p>I would like to understand the underlying behaviour because it feels as though I shouldn't, nor want to throw an exception within the SaveCart method.</p>
  
  <p>Here is the Test under question:</p>
  
  <pre><code>[TestMethod]
  [ExpectedException(typeof(ApplicationException))]
  public void CartRepoSaveCartExceptionShouldReturnZero()
  {
       _cartDatabaseMock.Setup(c =&gt; c.SaveCart(_cart))
                                     .Throws&lt;ApplicationException&gt;();
  
      var result = _cartRepository.SaveCart(_cart);
  
      Assert.AreEqual(result, _cartSaveExceptionValue);
  }
  </code></pre>
  
  <p>Here is the basic SaveCart which does NOT throw an exception causing the test to fail:</p>
  
  <pre><code>public long SaveCart(Cart cart )
  {
      long returnValue;
  
      try
      {
          returnValue = _cartDatabase.SaveCart(cart);
      }
      catch (Exception)
      {
          return 0;
      }
      return returnValue;
  }
  </code></pre>
  
  <p>Here is a basic SaveCart where the test works because it's throwing an exception:</p>
  
  <pre><code>public long SaveCart(Cart cart )
  {
      long returnValue;
  
      try
      {
          returnValue = _cartDatabase.SaveCart(cart);
      }
      catch (Exception)
      {
          throw new ApplicationException();
      }
      return returnValue;
  }
  </code></pre>
  
  <p>Feel free to suggest a better title for the question if it doesn't quite explain it clearly.</p>