Loading ...
Sorry, an error occurred while loading the content.

Exposing Myself

Expand Messages
  • Tim Haughton
    Twice today, I found myself exposing types in an assembly that I had no intention of exposing. More importantly, anyone coupling to these exposed interfaces
    Message 1 of 1 , Aug 5, 2005
    • 0 Attachment
      Twice today, I found myself exposing types in an assembly that I had
      no intention of exposing. More importantly, anyone coupling to these
      exposed interfaces would have unneccesarily added inertia to the
      design. To guard against my absent mindedness, I crafted a simple
      unit test to ensure only types I specifically allow are exposed.

      [Test]
      public void TestSurfaceArea()
      {
      Type exportedType = typeof(MyNameSpace.MyType);

      Type[] types = Assembly.GetExecutingAssembly
      ().GetExportedTypes();

      foreach ( Type t in types )
      {
      if ( t != exportedType )
      {
      object[] attrs = t.GetCustomAttributes( typeof
      (TestFixtureAttribute), false );
      string failureMessage = "The type " + t.Name
      + " should not be exposed.";
      Assert.AreEqual( 1, attrs.Length,
      failureMessage );

      failureMessage = "The test class " + t.Name +
      @" doesn't start with ""Test"".";
      Assert.IsTrue( t.Name.StartsWith( "Test" ),
      failureMessage );
      }
      }
      }


      I could just change Visual Studio's templates to default all members
      to internal/private, but that wouldn't cascade through to other
      developers.

      This method also checks that any NUnit tests that are exposed follow
      our convention and begin with the word "Test".

      I'm not sure that this is the best place for such a test, I'll have
      to run with it and see how we go. Any other suggestions or comments
      are of course welcome.

      --
      Regards,

      Tim Haughton

      Agitek
      http://agitek.co.uk
      http://blogitek.com/timhaughton
    Your message has been successfully submitted and would be delivered to recipients shortly.