Rudebox.co.nr How To Forums

Rudebox How To Forums - Ask Anything, View How To's
 
HomeFAQSearchRegisterMemberlistUsergroupsLog in

Share | 
 

 Unit Testing a Private Method using NUnit

View previous topic View next topic Go down 
AuthorMessage
Admin
Admin
avatar

Number of posts : 216
Registration date : 2007-01-01

PostSubject: Unit Testing a Private Method using NUnit   Sun Sep 09, 2007 11:34 pm

Source


First rule of thumb: Don't do this.

No really, Don't.

You are locking the internals of your code down with your unit test, if you are having to test a private method, then chances are that method should be public on a different class.

However, if you are sitting in a situation where that kind of refactoring is just not going to be allowed and you still really-really-need-to-get-this-under-test, then this simple process can help.



Given

Private Function Add2Integers(byval x as integer, byval y as integer) as integer

return x + y

End Function



You want to get this under test, but you want this to be a refactor (which is NOT another word for maintenance, it's a word for code clean up, not feature changing) which means no changes as far as the users are concerned.



Try this, wrap the code in a compiler conditional, and then just call the function, and then pass the value back out.

Now your unit tests can access the private method.

#If DEBUG (or something else if you have another compiler conditional) Then

Public Function public_Add2Integers(byval x as integer, byval y as integer) as integer

return Add2Integers( x, y) as integer

End Function
Back to top Go down
View user profile http://rudebox.editboard.com
 
Unit Testing a Private Method using NUnit
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» I.C.U. (Intensive Care Unit) Clan
» Most powerful 40k unit
» P1 Spec Lola-Jaguar Testing at Mont Serrat.
» Scoring Unit
» Pokemon Black Audino Method.

Permissions in this forum:You cannot reply to topics in this forum
Rudebox.co.nr How To Forums :: How To - Myspace / Website /Blog Utilities :: ASP .net-
Jump to: