Click here to Skip to main content
11,714,345 members (82,826 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: UnitTesting
Dear All



Is It possible to unit test, constructor in c#, if yes how if not why Plz response .

Thanks
Posted 26-Jun-13 3:23am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Member 10095292 at 26-Jun-13 9:38am
   
Thanks for Quick Response,,,

In case of Private Constructor is it Possible.......
aspnet -i at 26-Jun-13 9:50am
   
Nice question... Basically unit testing is about providing the input (to a class or method or anything) and verifying if it returns the expected result. Since the constructor in your question is 'private' , it cannot be called from outside the class. So no need to test it as per the principles of unit testing. Still if you want to unit test it, then call it inside a public method in the class and unit test this public method

Edit: private constructors are used only in static classes
Member 10095292 at 26-Jun-13 9:53am
   
Thanks a lot , this is nice post clear my doubt...


Thank you very much..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Yes, you do this by instantiating the object and then asserting the fields of the object are initialized properly. You can also assert that no exception was thrown during the constructor. You can check private fields through reflection if you need to.

In your case it depends on what you want to check in the unit test...
  Permalink  
Comments
Member 10095292 at 26-Jun-13 9:34am
   
Thanks for Response

In case of private Constructor is it possible?

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 540
1 Maciej Los 259
2 Richard MacCutchan 230
3 OriginalGriff 140
4 F-ES Sitecore 140
0 Sergey Alexandrovich Kryukov 545
1 CHill60 375
2 Maciej Los 324
3 Richard MacCutchan 310
4 OriginalGriff 260


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 26 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100