Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# reflection?

C# reflection?

Scheduled Pinned Locked Moved C#
questioncsharptestinghelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ppayal
    wrote on last edited by
    #1

    Hi, I have a class as under:

    public class ExampleClass : BaseClass

    {

        public int ExampleMethod(int \_toValidate)  {
    
              ExampleValidator(\_toValidate);
    
               return 1;
    

    }

    }

    The ExampleValidator method is contained in the BaseClass. It is used to validate the parameter _toValidate. The ExampleValidator check is very important and every method in the class ExampleClass should contain it. I want to create a unit test for this class ExampleClass, to test if every method does contain the ExampleValidator. How can I do that? Can reflection help me? Regards, ap.

    S 1 Reply Last reply
    0
    • P ppayal

      Hi, I have a class as under:

      public class ExampleClass : BaseClass

      {

          public int ExampleMethod(int \_toValidate)  {
      
                ExampleValidator(\_toValidate);
      
                 return 1;
      

      }

      }

      The ExampleValidator method is contained in the BaseClass. It is used to validate the parameter _toValidate. The ExampleValidator check is very important and every method in the class ExampleClass should contain it. I want to create a unit test for this class ExampleClass, to test if every method does contain the ExampleValidator. How can I do that? Can reflection help me? Regards, ap.

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      I think you are missing the point of unit testing. A unit test should run a piece of code and check that the output/result is as expected. A unit test does not examine code to check the semantics. What you should do is write several unit tests that run the ExampleMethod with different parameters and checked the results were correct. You would also write several unit tests that run the ExampleMethod with invalid parameters and check that the method failed in the correct way. What you are asking about doing is called "static analysis". MS have a tool called FxCop[^] which does static analysis. (Or if you have Vs2005/08 team versions, they have "code analysis" built in, which is basically the same as FxCop). If you google there are several tutorials for writing your own custom rules for FxCop. (see here[^]) There are plenty of other static analysis tools[^] if you want to try a few out. There is also DevPartner[^] from Compuware, which I've used in the past and you can add custom rules to.

      Simon

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups