title: Unit testing classes inheriting abstract class
body: <p>I have an abstract class like that:  </p>
  
  <pre><code>public abstract class Field&lt;T&gt;
  {
      private int _length;
      public int Length
      {
          get
          {
              return _length;
          }
          protected set
          {
              if (value &lt;= 0)
              {
                  throw new ArgumentOutOfRangeException("The length must be greater than 0");
              }
              else
              {
                  _length = value;
              }
          }
      }
  
      private T _value;
      public T Value 
      {
          get
          {
              if (_value == null) throw new ArgumentException("Field does not have any value set");
              return _value;
          }
          set
          {
              //here obviously I have some code to check the value and assign it to _value
              //I removed it though to show what the problem is
  
              throw new NotImplementedException();
          }
      }
  
      public Field(int length, T value)
      {
          Length = length;
          Value = value;
      }
  
      public Field(int length)
      {
          Length = length;
      }
  
      //some abstract methods irrelevant to the question...
  }
  </code></pre>
  
  <p>Then I have a class that inherits Field&lt;></p>
  
  <pre><code>public class StringField : Field&lt;string&gt;
  {
      public StringField(int length, string value)
          : base(length, value)
      { }
  
      public StringField(int length)
          : base(length)
      { }
  
      //implementation of some abstract methods irrelevant to the question...
  }
  </code></pre>
  
  <p>When I run a test like this, it is fine and passes (constructor throws a correct exception):</p>
  
  <pre><code>[TestMethod]
  [ExpectedException(typeof(ArgumentOutOfRangeException))]
  public void Constructor_LengthOnly_LengthZero_ShouldThrowArgumentOutOfRangeException()
  {
      int length = 0;
      StringField sf = new StringField(length);
  
      //should throw exception
  }
  </code></pre>
  
  <p>But when I run this test, the constructor does not throw, even though it should throw NotImplementedException:</p>
  
  <pre><code>[TestMethod]
  [ExpectedException(typeof(NotImplementedException))]
  public void Constructor_LengthAndValue_ValidLength_TextTooLong_ShouldThrowNotImplementedException()
  {
      int length = 2;
      string value = "test";
  
      StringField sf = new StringField(length, value);
  
      //should throw exception
  }
  </code></pre>
  
  <p>Am I doing something wrong? I don't think I am missing something, am I? Thanks.  </p>
  
  <p>--EDIT--  </p>
  
  <p>Turns out everything was ok, here what was happening:<br>
  - in the <code>Field</code> I had another property and constructor like so:</p>
  
  <pre><code>enprivate string _format;
  public string Format 
  { 
      get 
      { 
          return _format; 
      }
      protected set
      {
          _format = value;
       }
  }
  
  public Field(int length, string format)
  {
      Length = length;
      Format = format;
  }
  </code></pre>
  
  <p>- since the derived class was replacing <code>T</code> with <code>string</code>, I thought that by calling the base like I showed in my original message, I am calling the constructor that takes <code>Value</code>, but I was calling the one taking <code>Format</code>...<br>
  - to fix this, in my <code>StringField</code> class I replaced a call to the base constructor to look like this:</p>
  
  <pre><code>public StringField(int length, string value)
      : base(length, value: value)
  { }
  </code></pre>
  
  <p>An intresting case of type conflicts while using generics :)</p>