title: xUnit Assert.Throws and Record.Exception does not catch exception
body: <p>While writing unit test case to test thrown exception, have tried to use below both methods using xUnit</p>
  
  <ol>
  <li>Assert.Throws(action)</li>
  <li>Record.Exception(action)</li>
  </ol>
  
  <p>Following is the code</p>
  
  <pre><code>public class Me : Entity, IAggregateRoot
  {
     public string Name {get; }
  
     private List&lt;Friend&gt; friends;
     public IReadonlyCollection&lt;Friend&gt; Friends =&gt; friends;
  
     public Me(string name)
     {
        Name = name;
        friends = new List&lt;Friend&gt;();
     }
  
     public void AddFriend(Friend friend)
     {
       if(friend.Type != "UnKnown")
          friend.Add(friend);
       else
          throw new Exception("Cannot add a unknown friend.");
     }
  }
  
  public class Friend
  {
      public string Name {get; }
      public string Type {get; }
  
      public Friend(string name, string type)
      {
          Name = name;
          Type = type;
      }
  }
  
  
  using System;
  using MediatR;
  using System.Collections.Generic;
  
  public abstract class Entity
  {
      int? _requestedHashCode;
      int _Id;        
      public virtual  int Id 
      {
          get
          {
              return _Id;
          }
          protected set
          {
              _Id = value;
          }
      }
  
      private List&lt;INotification&gt; _domainEvents;
      public IReadOnlyCollection&lt;INotification&gt; DomainEvents =&gt; _ domainEvents?.AsReadOnly();
  
      public void AddDomainEvent(INotification eventItem)
      {
          _domainEvents = _domainEvents ?? new List&lt;INotification&gt;();
          _domainEvents.Add(eventItem);
      }
  
      public void RemoveDomainEvent(INotification eventItem)
      {
          _domainEvents?.Remove(eventItem);
      }
  
      public void ClearDomainEvents()
      {
          _domainEvents?.Clear();
      }
  
      public bool IsTransient()
      {
          return this.Id == default(Int32);
      }
  
      public override bool Equals(object obj)
      {
          if (obj == null || !(obj is Entity))
              return false;
  
          if (Object.ReferenceEquals(this, obj))
              return true;
  
          if (this.GetType() != obj.GetType())
              return false;
  
          Entity item = (Entity)obj;
  
          if (item.IsTransient() || this.IsTransient())
              return false;
          else
              return item.Id == this.Id;
      }
  
      public override int GetHashCode()
      {
          if (!IsTransient())
          {
              if (!_requestedHashCode.HasValue)
                  _requestedHashCode = this.Id.GetHashCode() ^ 31; 
  
              return _requestedHashCode.Value;
          }
          else
              return base.GetHashCode();
  
      }
      public static bool operator ==(Entity left, Entity right)
      {
          if (Object.Equals(left, null))
              return (Object.Equals(right, null)) ? true : false;
          else
              return left.Equals(right);
      }
  
      public static bool operator !=(Entity left, Entity right)
      {
          return !(left == right);
      }
  }
  
  public interface IAggregateRoot
  {}//its empty
  </code></pre>
  
  <p>Test Case 1: Using Assert.Throws</p>
  
  <pre><code>[Fact]
  public void add_friend_business_rule_validation_throws_exception1()
  {
     var me = new Me("mady");
     var friend = new Friend("johnDoe","UnKnown");
  
     Assert.Throws&lt;Exception&gt;(() =&gt; me.AddFriend(friend));
  }
  </code></pre>
  
  <p>Test Case 2: Using Record.Exception</p>
  
  <pre><code>[Fact]
  public void add_friend_business_rule_validation_throws_exception()
  {
     var me = new Me("mady");
     var friend = new Friend("johnDoe","UnKnown");
  
     var ex = Record.Exception(() =&gt; me.AddFriend(friend));
     Assert.NotNull(ex);
     Assert.IsType&lt;Exception&gt;(ex);
  }
  </code></pre>
  
  <p>I would like to write a unit test case to test exception for the provided code. My expectation is the code should throw exception on validation fail when trying to add a entity to a collection. </p>
  
  <p><strong>Issue:</strong> The test cases here are not failing neither passing. The execution is stalled at following line.</p>
  
  <pre><code>  throw new Exception("Cannot add a unknown friend.");
  </code></pre>
  
  <p>This will happen when you have no try catch block to catch it. I'm expecting xUnit Assert.Throws &amp; Record.Exception will catch the exception but it is not.</p>
  
  <p><strong>Note:</strong> The code provided is very close to the actual code, with entity names being different and simplified by removing unnecessary code.</p>
  
  <p>I have seen the following documentation</p>
  
  <ol>
  <li><a href="https://github.com/xunit/xunit/blob/master/src/xunit.core/Record.cs#L19" rel="nofollow noreferrer">xUnit-Record.Exception Documentation</a></li>
  <li><a href="https://stackoverflow.com/questions/45017295/assert-an-exception-using-xunit">Assert an Exception using XUnit- Stackoverflow question</a></li>
  </ol>
  
  <p><strong>EDIT:</strong>
  <strong>The xUnit Assert.Throws &amp; Record.Exception behavior is as expected while I Run Tests.</strong></p>
  
  <p><strong>Issue is with debugging tests.</strong></p>
  
  <p>As mentioned test cases were are not failing neither passing. Whenever any line of code throws exception the nearest catch block shall catch the exception. I have similar expectation from xUnit Assert.Throws &amp; Record.Exception based on documentation and user experience. However this is not the case while debugging test.</p>
  
  <p>An alternate &amp; well known approach to demonstrate the problem and expectation of behavior.</p>
  
  <pre><code>[Fact]
  public void add_friend_business_rule_validation_throws_exception1()
  {
     var me = new Me("mady");
     var friend = new Friend("johnDoe","UnKnown");
     Exception expectedException = null;
     try
     { 
         me.AddFriend(friend); //exception thrown block
     }
     catch(Exception ex)//exception catched - control not stopped 
     {
         expectedException = ex;
     }
  
     Assert.NotNull(expectedException);
     Assert.IsType&lt;Exception&gt;(expectedException);
  }
  </code></pre>