tags:

views:

48

answers:

4

I have a private method like below:

int void SomeMethod(ref string theStr)
{
   // Some Implementation
}

how to write the unit test case for this method.

+3  A: 

Seems a bit pointless that a method is void but takes a ref parameter. It would probably make sense to make it return a string:

public class FooBar {
 internal string SomeMethod(ref string theStr) { 
    // Some Implementation 
    return theStr;
 }
}

We also make it internal and specify InternalVisibleTo attribute in AssemblyInfo.cs file:

 [assembly: InternalsVisibleTo("Test.Assembly")]

This way SomeMethod will behave as though it's internal (i.e. not visible outside its assembly) except for Test.Assembly, which will see it as public.

The unit test is pretty trivial (regardless of whether or not it takes a ref parameter).

[Test]
public void SomeMethodShouldReturnSomething() { 
   Foobar foobar = new Foobar();
   string actual;
   foobar.SomeMethod(ref actual);
   Assert.AreEqual("I'm the test your tests could smell like", actual);
}
Igor Zevaka
+1: that's nice.
Rob
Thanks, for your valuable correction, but here i have just gave one code snipt, this is not the actual code. My intension is to know the way to test a private method with ref keyword.
Nikhil Verma
@Nikhil: If you don't give representative code, we can't easily tell which differences are relevant and which aren't.
Jon Skeet
@Nikhil Verma It doesn't really matter if the method takes a ref param. See updated code.
Igor Zevaka
The thing I don't like about this (although I have done it) is that you're making your main assembly aware of your testing assembly, by putting 'InternalsVisibleTo' in it.
Grant Crofton
+1  A: 

I usually make the method protected and provide a testable class that inherits. For example:

class Foo
{
  protected void SomeMethod(ref string theStr) { ... }
  ...
}

class TestableFoo
{
  public void TestableSomeMethod(ref string theStr)
  {
    base.SomeMethod(...);
  }
  ...

I think you'll find answers that say "you shouldn't test private methods", but there are cases where I've found it to be useful to get at some tricky functionality. But then, I've also found that in these situations it's better to extract the function into it's own separate testable class. ymmv.

Rob
I'd actually argue not that you don't "need" to test private methods, but rather than you shouldn't, as it's not part of the public contract of the class.
kyoryu
Good point, I'll edit.
Rob
@kyoryu That's very true. I usually draw a line between unit testing an internal method and taking the functionality into another class when there is not enough code to make it a class. I am happy to dilute the purity of the test for cases like these.
Igor Zevaka
@kyoryu: I often find that it's more effective to test internals of a class than limit myself to the public API. If it's a relatively small public API but with several potential corner cases, white box testing can make it a lot easier to test those corner cases without having to set up the rest of what would be required for the public API.
Jon Skeet
I am agree with all of you. But in some situation it is necessary to test the private method also. If any body interested i can give the example also.
Nikhil Verma
A: 

Hi Guys,

My Question was how to write the unit test case for the private method which is having the ref parameter.

Some of says that change the implementation, it is not possible. might be i have given some wrong implementation in code snipt but that was not my intension.

Some says that no need to test the private method. but in some scenario, it is required to test those method such as in some safety code.

Answer is: I need to use the reflection and set the setnamedparameterAction from the nunit. i need to specify explicitly that perticular parameter is ref.

Nikhil Verma
A: 

If it's that important to test it, maybe you should just make it public and be done with it. (Although I realise that doesn't exactly answer your question).

Not everyone would agree with making something public just for testing, but I think it's better than some of the alternatives.

Grant Crofton