NUnit: how to check private method with parameter "ref" in C #

I have a private method as shown below:

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

how to write a unit test case for this method.

+3
source share
4 answers

It seems a little pointless that the method is invalid but accepts a parameter ref. It would probably be advisable to return the line:

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

We also do it internaland specify the attribute InternalVisibleToin the AssemblyInfo.cs file:

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

This way it SomeMethodwill behave as if it were internal (i.e. not visible outside its assembly), with the exception of Test.Assembly, which will see it as public.

unit test ( , ref).

[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);
}
+7

, . :

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

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

, , : " ", , . , , . YMMV.

+1

, unit test , ref.

, . , snipt , .

, . , , .

: setnamedparameterAction nunit. , perticular .

0

, , . ( , ).

- , , , .

0

Source: https://habr.com/ru/post/1754754/


All Articles